Skip to content

Commit 09f815d

Browse files
author
Ali Lloyd
committed
partially refactored Bitmap Effects getting & setting
1 parent de069e4 commit 09f815d

File tree

14 files changed

+1016
-118
lines changed

14 files changed

+1016
-118
lines changed

engine/src/bitmapeffect.cpp

Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,370 @@ void MCBitmapEffectsComputeBounds(MCBitmapEffectsRef self, const MCRectangle& p_
13451345

13461346
////////////////////////////////////////////////////////////////////////////////
13471347

1348+
static bool MCBitmapEffectLookup(MCBitmapEffectsRef& self, MCNameRef p_prop, Properties which, MCBitmapEffectProperty& r_prop, MCBitmapEffect*& r_effect)
1349+
{
1350+
// First map the property type
1351+
MCBitmapEffectType t_type;
1352+
t_type = (MCBitmapEffectType)(which - P_BITMAP_EFFECT_DROP_SHADOW);
1353+
1354+
// Now fetch the bitmap effect we are processing - note that if this is
1355+
// NULL it means it isn't set. In this case we still carry on since we
1356+
// need to report a invalid key error (if applicable).
1357+
MCBitmapEffect *t_effect;
1358+
if (self != nil && (self -> mask & (1 << t_type)) != 0)
1359+
r_effect = &self -> effects[t_type];
1360+
else
1361+
r_effect = nil;
1362+
1363+
if (MCBitmapEffectLookupProperty(t_type, p_prop, r_prop) != ES_NORMAL)
1364+
return false;
1365+
1366+
return true;
1367+
}
1368+
1369+
1370+
static bool MCBitmapEffectLookupForSet(MCBitmapEffectsRef& self, MCNameRef p_prop, Properties which, MCBitmapEffectProperty& r_prop, MCBitmapEffect& r_effect, MCBitmapEffectType& r_type, bool& r_dirty)
1371+
{
1372+
r_type = (MCBitmapEffectType)(which - P_BITMAP_EFFECT_DROP_SHADOW);
1373+
1374+
// Now fetch the bitmap effect we are processing - note that if this is
1375+
// NULL it means it isn't set. In this case we still carry on since we
1376+
// need to report a invalid key error (if applicable).
1377+
if (self != nil && (self -> mask & (1 << r_type)) != 0)
1378+
{
1379+
r_effect = self -> effects[r_type];
1380+
r_dirty = false;
1381+
}
1382+
else
1383+
{
1384+
MCBitmapEffectDefault(&r_effect, r_type);
1385+
// If the effect doesn't yet exist, it means we will dirty the object
1386+
// regardless.
1387+
r_dirty = true;
1388+
}
1389+
1390+
// Lookup the property and ensure it is appropriate for our type.
1391+
if (MCBitmapEffectLookupProperty(r_type, p_prop, r_prop) != ES_NORMAL)
1392+
return false;
1393+
}
1394+
1395+
static bool MCBitmapEffectCommitChanges(MCBitmapEffectsRef& self, MCBitmapEffectType p_type, MCBitmapEffect p_new_effect)
1396+
{
1397+
// If we are currently empty, then allocate a new object
1398+
if (self == nil)
1399+
{
1400+
self = new MCBitmapEffects;
1401+
if (self == nil)
1402+
return false;
1403+
1404+
// Only need to initialize the mask.
1405+
self -> mask = 0;
1406+
}
1407+
1408+
// Now copy in the updated effect.
1409+
self -> mask |= (1 << p_type);
1410+
self -> effects[p_type] = p_new_effect;
1411+
}
1412+
1413+
bool MCBitmapEffectsGetColorProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, MCColor*& r_color)
1414+
{
1415+
MCBitmapEffectProperty t_prop;
1416+
MCBitmapEffect* effect;
1417+
if (MCBitmapEffectLookup(self, p_index, which, t_prop, effect))
1418+
{
1419+
if (effect == nil)
1420+
{
1421+
r_color = nil;
1422+
return true;
1423+
}
1424+
1425+
MCColor t_color;
1426+
switch(t_prop)
1427+
{
1428+
case kMCBitmapEffectPropertyColor:
1429+
MCBitmapEffectColorToMCColor(effect->layer.color, *r_color);
1430+
break;
1431+
1432+
default:
1433+
break;
1434+
}
1435+
return true;
1436+
}
1437+
return false;
1438+
}
1439+
1440+
bool MCBitmapEffectsGetEnumProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, intenum_t*& r_value)
1441+
{
1442+
MCBitmapEffectProperty t_prop;
1443+
MCBitmapEffect* effect;
1444+
if (MCBitmapEffectLookup(self, p_index, which, t_prop, effect))
1445+
{
1446+
if (effect == nil)
1447+
{
1448+
r_value = nil;
1449+
return true;
1450+
}
1451+
1452+
switch(t_prop)
1453+
{
1454+
case kMCBitmapEffectPropertyBlendMode:
1455+
*r_value = (intenum_t)effect->layer.blend_mode;
1456+
break;
1457+
1458+
case kMCBitmapEffectPropertyFilter:
1459+
*r_value = (intenum_t)effect->blur.filter;
1460+
break;
1461+
1462+
case kMCBitmapEffectPropertySource:
1463+
*r_value = effect -> glow . source;
1464+
break;
1465+
1466+
default:
1467+
break;
1468+
}
1469+
return true;
1470+
}
1471+
return false;
1472+
}
1473+
1474+
bool MCBitmapEffectsGetUIntProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, uinteger_t*& r_uint)
1475+
{
1476+
MCBitmapEffectProperty t_prop;
1477+
MCBitmapEffect* effect;
1478+
if (MCBitmapEffectLookup(self, p_index, which, t_prop, effect))
1479+
{
1480+
if (effect == nil)
1481+
{
1482+
r_uint = nil;
1483+
return true;
1484+
}
1485+
1486+
switch(t_prop)
1487+
{
1488+
case kMCBitmapEffectPropertyOpacity:
1489+
*r_uint = MCGPixelGetNativeAlpha(effect -> layer . color);
1490+
break;
1491+
1492+
case kMCBitmapEffectPropertySize:
1493+
*r_uint = effect -> blur . size;
1494+
break;
1495+
1496+
case kMCBitmapEffectPropertySpread:
1497+
*r_uint = effect -> blur . spread;
1498+
break;
1499+
1500+
case kMCBitmapEffectPropertyDistance:
1501+
*r_uint = effect -> shadow . distance;
1502+
break;
1503+
1504+
case kMCBitmapEffectPropertyAngle:
1505+
*r_uint = effect -> shadow . angle;
1506+
break;
1507+
1508+
case kMCBitmapEffectPropertyRange:
1509+
*r_uint = effect -> glow . range;
1510+
break;
1511+
1512+
default:
1513+
break;
1514+
}
1515+
return true;
1516+
}
1517+
return false;
1518+
}
1519+
1520+
bool MCBitmapEffectsGetBoolProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, bool*& r_bool)
1521+
{
1522+
MCBitmapEffectProperty t_prop;
1523+
MCBitmapEffect* effect;
1524+
if (MCBitmapEffectLookup(self, p_index, which, t_prop, effect))
1525+
{
1526+
if (effect == nil)
1527+
{
1528+
r_bool = nil;
1529+
return true;
1530+
}
1531+
1532+
switch(t_prop)
1533+
{
1534+
case kMCBitmapEffectPropertyKnockOut:
1535+
*r_bool = effect -> shadow . knockout;
1536+
break;
1537+
1538+
default:
1539+
break;
1540+
}
1541+
return true;
1542+
}
1543+
return false;
1544+
}
1545+
1546+
bool MCBitmapEffectsSetColorProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, MCColor p_color, bool& r_dirty)
1547+
{
1548+
MCBitmapEffectProperty t_prop;
1549+
MCBitmapEffect t_effect;
1550+
MCBitmapEffectType t_type;
1551+
bool t_dirty;
1552+
if (MCBitmapEffectLookupForSet(self, p_index, which, t_prop, t_effect, t_type, t_dirty))
1553+
{
1554+
switch (t_prop)
1555+
{
1556+
case kMCBitmapEffectPropertyColor:
1557+
{
1558+
uint4 t_new_color;
1559+
t_new_color = MCGPixelPackNative(p_color.red >> 8, p_color.green >> 8, p_color.blue >> 8, MCGPixelGetNativeAlpha(t_effect . layer . color));
1560+
1561+
if (t_new_color != t_effect . layer . color)
1562+
{
1563+
t_effect . layer . color = t_new_color;
1564+
t_dirty = true;
1565+
}
1566+
}
1567+
break;
1568+
1569+
default:
1570+
break;
1571+
}
1572+
1573+
if (!t_dirty || MCBitmapEffectCommitChanges(self, t_type, t_effect))
1574+
{
1575+
r_dirty = t_dirty;
1576+
return true;
1577+
}
1578+
}
1579+
return false;
1580+
}
1581+
1582+
bool MCBitmapEffectsSetEnumProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, intenum_t p_value, bool& r_dirty)
1583+
{
1584+
MCBitmapEffectProperty t_prop;
1585+
MCBitmapEffect effect;
1586+
MCBitmapEffectType t_type;
1587+
bool t_dirty;
1588+
if (MCBitmapEffectLookupForSet(self, p_index, which, t_prop, effect, t_type, t_dirty))
1589+
{
1590+
switch (t_prop)
1591+
{
1592+
case kMCBitmapEffectPropertyBlendMode:
1593+
{
1594+
MCBitmapEffectBlendMode t_new_mode;
1595+
t_new_mode = (MCBitmapEffectBlendMode)p_value;
1596+
if (t_new_mode != effect . layer . blend_mode)
1597+
{
1598+
effect . layer . blend_mode = t_new_mode;
1599+
t_dirty = true;
1600+
}
1601+
}
1602+
break;
1603+
1604+
case kMCBitmapEffectPropertyFilter:
1605+
{
1606+
MCBitmapEffectFilter t_new_filter;
1607+
t_new_filter = (MCBitmapEffectFilter)p_value;
1608+
if (t_new_filter != effect . blur . filter)
1609+
{
1610+
effect . blur . filter = t_new_filter;
1611+
t_dirty = true;
1612+
}
1613+
}
1614+
break;
1615+
1616+
default:
1617+
break;
1618+
}
1619+
1620+
if (!t_dirty || MCBitmapEffectCommitChanges(self, t_type, effect))
1621+
{
1622+
r_dirty = t_dirty;
1623+
return true;
1624+
}
1625+
}
1626+
return false;
1627+
}
1628+
1629+
bool MCBitmapEffectsSetUIntProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, uinteger_t p_uint, bool& r_dirty)
1630+
{
1631+
MCBitmapEffectProperty t_prop;
1632+
MCBitmapEffect effect;
1633+
MCBitmapEffectType t_type;
1634+
bool t_dirty;
1635+
if (MCBitmapEffectLookupForSet(self, p_index, which, t_prop, effect, t_type, t_dirty))
1636+
{
1637+
switch (t_prop)
1638+
{
1639+
case kMCBitmapEffectPropertyOpacity:
1640+
{
1641+
p_uint = MCU_min(p_uint, (uint4)255);
1642+
if (p_uint != MCGPixelGetNativeAlpha(effect . layer . color))
1643+
{
1644+
uint8_t r, g, b, a;
1645+
MCGPixelUnpackNative(effect . layer . color, r, g, b, a);
1646+
1647+
effect . layer . color = MCGPixelPackNative(r, g, b, p_uint);
1648+
t_dirty = true;
1649+
}
1650+
}
1651+
break;
1652+
1653+
case kMCBitmapEffectPropertySize:
1654+
{
1655+
p_uint = MCU_min(p_uint, (uint4)255);
1656+
if (p_uint != effect . blur . size)
1657+
{
1658+
effect . blur . size = p_uint;
1659+
t_dirty = true;
1660+
}
1661+
}
1662+
break;
1663+
1664+
case kMCBitmapEffectPropertySpread:
1665+
{
1666+
p_uint = MCU_min(p_uint, (uint4)255);
1667+
if (p_uint != effect . blur . spread)
1668+
{
1669+
effect . blur . spread = p_uint;
1670+
t_dirty = true;
1671+
}
1672+
}
1673+
break;
1674+
1675+
case kMCBitmapEffectPropertyDistance:
1676+
{
1677+
p_uint = MCU_min(p_uint, (uint4)32767);
1678+
if (p_uint != effect . shadow . distance)
1679+
{
1680+
effect . shadow . distance = p_uint;
1681+
t_dirty = true;
1682+
}
1683+
}
1684+
break;
1685+
1686+
case kMCBitmapEffectPropertyAngle:
1687+
{
1688+
p_uint %= 360;
1689+
if (p_uint != effect . shadow . angle)
1690+
{
1691+
effect . shadow . angle = p_uint;
1692+
t_dirty = true;
1693+
}
1694+
}
1695+
break;
1696+
1697+
default:
1698+
return false;
1699+
}
1700+
1701+
if (!t_dirty || MCBitmapEffectCommitChanges(self, t_type, effect))
1702+
{
1703+
r_dirty = t_dirty;
1704+
return true;
1705+
}
1706+
}
1707+
return false;
1708+
}
1709+
1710+
////////////////////////////////////////////////////////////////////////////////
1711+
13481712
#ifdef OLD_GRAPHICS
13491713
PACKED_INLINE uint8_t _scale_bounded(uint8_t a, uint8_t b)
13501714
{

engine/src/bitmapeffect.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ bool MCBitmapEffectsSetProperties(MCBitmapEffectsRef& self, Properties which_typ
3939
bool MCBitmapEffectsGetPropertyElement(MCBitmapEffectsRef& self, Properties which_type, MCNameRef p_prop, MCValueRef& r_setting);
4040
bool MCBitmapEffectsGetProperties(MCBitmapEffectsRef& self, Properties which_type, MCArrayRef& r_props);
4141

42+
bool MCBitmapEffectsGetColorProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, MCColor*& r_color);
43+
bool MCBitmapEffectsGetEnumProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, intenum_t*& r_value);
44+
bool MCBitmapEffectsGetUIntProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, uinteger_t*& r_value);
45+
46+
bool MCBitmapEffectsSetColorProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, MCColor p_color, bool& r_dirty);
47+
bool MCBitmapEffectsSetEnumProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, intenum_t p_value, bool& r_dirty);
48+
bool MCBitmapEffectsSetUIntProperty(MCBitmapEffectsRef& self, MCNameRef p_index, Properties which, uinteger_t p_value, bool& r_dirty);
49+
4250
uint32_t MCBitmapEffectsWeigh(MCBitmapEffectsRef self);
4351
IO_stat MCBitmapEffectsPickle(MCBitmapEffectsRef self, MCObjectOutputStream& p_stream);
4452
IO_stat MCBitmapEffectsUnpickle(MCBitmapEffectsRef& self, MCObjectInputStream& p_stream);

0 commit comments

Comments
 (0)