LEDs allow text on the left side

This commit is contained in:
2024-11-26 10:53:45 -06:00
committed by Celtic Minstrel
parent 121f54fbe2
commit c35ba7cc62
4 changed files with 34 additions and 10 deletions

View File

@@ -63,6 +63,12 @@
<xs:enumeration value="led"/> <xs:enumeration value="led"/>
</xs:restriction> </xs:restriction>
</xs:simpleType> </xs:simpleType>
<xs:simpleType name="textSide">
<xs:restriction base="xs:token">
<xs:enumeration value="left"/>
<xs:enumeration value="right"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="frameStyle"> <xs:simpleType name="frameStyle">
<xs:restriction base="xs:token"> <xs:restriction base="xs:token">
<xs:enumeration value="solid"/> <xs:enumeration value="solid"/>
@@ -230,6 +236,7 @@
<xs:attributeGroup ref="rect-size"/> <xs:attributeGroup ref="rect-size"/>
<xs:attributeGroup ref="position"/> <xs:attributeGroup ref="position"/>
<xs:attributeGroup ref="font"/> <xs:attributeGroup ref="font"/>
<xs:attribute name="text-side" default="right" type="textSide"/>
<xs:attribute name="wrap" default="false" type="bool"/> <xs:attribute name="wrap" default="false" type="bool"/>
</xs:extension> </xs:extension>
</xs:simpleContent> </xs:simpleContent>

View File

@@ -86,7 +86,7 @@ bool cLed::manageFormat(eFormat prop, bool set, boost::any* val) {
return true; return true;
} }
const int LED_TEXT_OFFSET = 18; // Possibly could be 20 const int LED_TEXT_SPACE = 4; // Possibly could be 6
void cLed::draw(){ void cLed::draw(){
rectangle from_rect, to_rect; rectangle from_rect, to_rect;
@@ -98,15 +98,27 @@ void cLed::draw(){
style.pointSize = textSize; style.pointSize = textSize;
style.lineHeight = textSize - 1; style.lineHeight = textSize - 1;
style.font = textFont; style.font = textFont;
style.colour = textClr;
from_rect = ledRects[state][depressed]; from_rect = ledRects[state][depressed];
to_rect = frame; to_rect = frame;
to_rect.right = to_rect.left + 14; int text_width = string_length(getText(), style);
to_rect.bottom = to_rect.top + 10; if(textOnRight){
rect_draw_some_item(*ResMgr::graphics.get(buttons[btnGW[BTN_LED]]),from_rect,*inWindow,to_rect); to_rect.right = to_rect.left + from_rect.width();
style.colour = textClr; to_rect.bottom = to_rect.top + from_rect.height();
to_rect.right = frame.right; rect_draw_some_item(*ResMgr::graphics.get(buttons[btnGW[BTN_LED]]),from_rect,*inWindow,to_rect);
to_rect.left = frame.left + LED_TEXT_OFFSET; }else{
win_draw_string(*inWindow,to_rect,getText(),wrapLabel ? eTextMode::WRAP : eTextMode::LEFT_TOP,style); to_rect.right = text_width;
win_draw_string(*inWindow,to_rect,getText(),wrapLabel ? eTextMode::WRAP : eTextMode::LEFT_TOP,style);
}
if(textOnRight){
to_rect.left = frame.left + from_rect.width() + LED_TEXT_SPACE;
to_rect.right = frame.right;
win_draw_string(*inWindow,to_rect,getText(),wrapLabel ? eTextMode::WRAP : eTextMode::LEFT_TOP,style);
}else{
to_rect.left = frame.left + text_width + LED_TEXT_SPACE;
to_rect.right = to_rect.left + from_rect.width();
rect_draw_some_item(*ResMgr::graphics.get(buttons[btnGW[BTN_LED]]),from_rect,*inWindow,to_rect);
}
} }
inWindow->setActive(); inWindow->setActive();
@@ -136,13 +148,16 @@ eLedState cLed::getState() const {
} }
bool cLed::parseAttribute(ticpp::Attribute& attr, std::string tagName, std::string fname) { bool cLed::parseAttribute(ticpp::Attribute& attr, std::string tagName, std::string fname) {
if(attr.Name() == "state") { if(attr.Name() == "state"){
std::string val = attr.Value(); std::string val = attr.Value();
if(val == "red") setState(led_red); if(val == "red") setState(led_red);
else if(val == "green") setState(led_green); else if(val == "green") setState(led_green);
else if(val == "off") setState(led_off); else if(val == "off") setState(led_off);
else throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname); else throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname);
return true; return true;
}else if(attr.Name() == "text-side"){
textOnRight = (attr.Value() == "right");
return true;
} }
return cButton::parseAttribute(attr, tagName, fname); return cButton::parseAttribute(attr, tagName, fname);
} }
@@ -158,7 +173,7 @@ location cLed::getPreferredSize() const {
if(!getText().empty()){ if(!getText().empty()){
TextStyle style; TextStyle style;
style.pointSize = textSize; style.pointSize = textSize;
width = LED_TEXT_OFFSET + string_length(getText(), style); width = LED_TEXT_SPACE + ledRects[state][depressed].width(); + string_length(getText(), style);
} }
return {width, ledRects[0][0].height()}; return {width, ledRects[0][0].height()};
} }

View File

@@ -60,6 +60,7 @@ private:
bool manageFormat(eFormat prop, bool set, boost::any* val) override; bool manageFormat(eFormat prop, bool set, boost::any* val) override;
eLedState state; eLedState state;
eFont textFont; eFont textFont;
bool textOnRight = true;
static rectangle ledRects[3][2]; static rectangle ledRects[3][2];
}; };

View File

@@ -152,6 +152,7 @@ Attributes** above.
* `state` - Specifies the starting state of the LED. Can be one of * `state` - Specifies the starting state of the LED. Can be one of
`red`, `green`, or `off`; defaults to `off`. `red`, `green`, or `off`; defaults to `off`.
* `font`, `size`, `color`, `colour` - See **Common Attributes** above. Note that, for an LED, omitting the size attribute gives a different result than any of the possible values. * `font`, `size`, `color`, `colour` - See **Common Attributes** above. Note that, for an LED, omitting the size attribute gives a different result than any of the possible values.
* `text-side` - defaults to `right`, `left` switches the horizontal order of the LED and its text
The `<group>` tag The `<group>` tag
----------------- -----------------