MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / buttonHandler

Method buttonHandler

source/windowing/StarWidgetParsing.cpp:123–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

121}
122
123WidgetConstructResult WidgetParser::buttonHandler(String const& name, Json const& config) {
124 String baseImage;
125 bool invisible = config.getBool("invisible", false);
126
127 try {
128 if (!invisible)
129 baseImage = config.getString("base");
130 } catch (MapException const& e) {
131 throw WidgetParserException(
132 strf("Malformed gui json, missing a required value in the map. {}", outputException(e, false)));
133 }
134
135 String hoverImage = config.getString("hover", "");
136 String pressedImage = config.getString("pressed", "");
137 String disabledImage = config.getString("disabledImage", "");
138
139 String baseImageChecked = config.getString("baseImageChecked", "");
140 String hoverImageChecked = config.getString("hoverImageChecked", "");
141 String pressedImageChecked = config.getString("pressedImageChecked", "");
142 String disabledImageChecked = config.getString("disabledImageChecked", "");
143
144 String callback = config.getString("callback", name);
145
146 if (!m_callbacks.contains(callback))
147 throw WidgetParserException::format("Failed to find callback named: {}", callback);
148 WidgetCallbackFunc callbackFunc = m_callbacks.get(callback);
149
150 auto button = make_shared<ButtonWidget>(callbackFunc, baseImage, hoverImage, pressedImage, disabledImage);
151 button->setCheckedImages(baseImageChecked, hoverImageChecked, pressedImageChecked, disabledImageChecked);
152 common(button, config);
153
154 button->setInvisible(invisible);
155
156 if (config.contains("caption"))
157 button->setText(config.getString("caption"));
158
159 if (config.contains("pressedOffset"))
160 button->setPressedOffset(jsonToVec2I(config.get("pressedOffset")));
161
162 if (config.contains("textOffset"))
163 button->setTextOffset(jsonToVec2I(config.get("textOffset")));
164
165 if (config.contains("checkable"))
166 button->setCheckable(config.getBool("checkable"));
167
168 if (config.contains("checked"))
169 button->setChecked(config.getBool("checked"));
170
171 String hAnchor = config.getString("textAlign", "center");
172 if (hAnchor == "right") {
173 button->setTextAlign(HorizontalAnchor::RightAnchor);
174 } else if (hAnchor == "center") {
175 button->setTextAlign(HorizontalAnchor::HMidAnchor);
176 } else if (hAnchor == "left") {
177 button->setTextAlign(HorizontalAnchor::LeftAnchor);
178 } else {
179 throw WidgetParserException(strf(
180 "Malformed gui json, expected textAlign to be one of \"left\", \"right\", or \"center\", got {}", hAnchor));

Callers

nothing calls this directly

Calls 15

strfFunction · 0.85
jsonToVec2IFunction · 0.85
jsonToColorFunction · 0.85
getBoolMethod · 0.80
getStringMethod · 0.80
setCheckedImagesMethod · 0.80
setInvisibleMethod · 0.80
setPressedOffsetMethod · 0.80
setTextOffsetMethod · 0.80
setCheckableMethod · 0.80
setCheckedMethod · 0.80

Tested by

no test coverage detected