Skip to content

Commit a4ac78b

Browse files
author
Jonas Trampe
committed
Set max/min values of buttons width/height and validate it
1 parent 9edf1e9 commit a4ac78b

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

webserver/static/js/custom_blocks_events.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,43 @@ Blockly.Blocks.event_button_object = {
8282
init: function () {
8383
this.appendDummyInput()
8484
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON']);
85-
this.appendValueInput('X')
86-
.setCheck('Number')
87-
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_X']);
88-
this.appendValueInput('Y')
89-
.setCheck('Number')
90-
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_Y']);
91-
this.appendValueInput('Width')
92-
.setCheck('Number')
93-
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_WIDTH']);
94-
this.appendValueInput('Height')
95-
.setCheck('Number')
96-
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_HEIGHT']);
97-
this.setInputsInline(true);
85+
this.appendDummyInput('Values')
86+
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_X'])
87+
.appendField(new Blockly.FieldNumber(0, 0, 9, 1), 'X')
88+
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_Y'])
89+
.appendField(new Blockly.FieldNumber(0, 0, 9, 1), 'Y')
90+
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_WIDTH'])
91+
.appendField(new Blockly.FieldNumber(1, 1, 10, 1), 'Width')
92+
.appendField(Blockly.Msg['HUCON_EVENTS_BUTTON_HEIGHT'])
93+
.appendField(new Blockly.FieldNumber(1, 1, 10 ,1), 'Height');
94+
// this.setInputsInline(true);
9895
this.appendStatementInput('function')
9996
.setCheck(null)
10097
.appendField(new Blockly.FieldTextInput(Blockly.Msg['HUCON_EVENTS_FUNC']), 'EventName');
10198
this.setColour(Blockly.Msg['HUCON_EVENTS_HUE']);
10299

103100
var thisBlock = this;
104-
this.setTooltip(function() {
101+
this.setTooltip(function () {
105102
return Blockly.Msg['HUCON_EVENTS_BUTTON_TOOLTIP_1'] + thisBlock.getFieldValue('EventName') + Blockly.Msg['HUCON_EVENTS_BUTTON_TOOLTIP_2'];
106103
});
107-
}
104+
105+
this.setOnChange(function (changeEvent) {
106+
if (changeEvent.type === Blockly.Events.BLOCK_CHANGE) {
107+
let values = this.getInput('Values')
108+
109+
let x = values.fieldRow.find(({name}) => name === 'X').getValue()
110+
let y = values.fieldRow.find(({name}) => name === 'Y').getValue()
111+
let width = values.fieldRow.find(({name}) => name === 'Width').getValue()
112+
let height = values.fieldRow.find(({name}) => name === 'Height').getValue()
113+
114+
if ((x + width) > 10)
115+
values.fieldRow.find(({name}) => name === 'Width').setValue(10 - x);
116+
117+
if ((y + height) > 10)
118+
values.fieldRow.find(({name}) => name === 'Height').setValue(10 - y);
119+
}
120+
});
121+
},
108122
};
109123
Blockly.Python.event_button_object = function(block) {
110124
// Catch all global variables.

0 commit comments

Comments
 (0)