Colouring dynamic variables based on name

117 views
Skip to first unread message

Eva Nolan

unread,
Mar 19, 2018, 1:04:51 PM3/19/18
to Blockly

I’m trying to colour new variables differently by their names in order to make it easier for a user to identify where each of their variables are being used in their program. I am currently calling my recolour function in the createVariableButtonHandler. This works if the variables are instantiated in the workspace but when a new variable is created the colour of all of the variables in the toolbox are overwritten. Is there a way in which I can separately colour for example the variable getter block based on the user inputted name of that block? 


Rachel Fenichel

unread,
Mar 19, 2018, 9:30:57 PM3/19/18
to Blockly
I suspect in the current state if you change the value in the dropdown when the block is on the workspace, the color won't change.

You could redefine the variable blocks to do what you need, or you might be able to add a change listener on the playground that watches for changes in your variable blocks and updates their colors accordingly.

Rachel
Message has been deleted

Eva Nolan

unread,
Mar 26, 2018, 12:11:22 PM3/26/18
to Blockly

Thank you Rachel, I have the colours being updated in the change and create block event listeners. This works perfectly in the workspace but not in the toolbox. 

In the toolbox I am colouring the blocks by type using a function I found in a previous thread (shown below). This is ok for the math change and variables set blocks as I can colour them based on the colour of firstVariable (I have a dictionary matching variable Ids to hue values) in Blockly.Variables.flyoutCategoryBlocks. This wont work for the getter block as there is more than one in the toolbox. 

Is there a way of colouring getter blocks in the toolbox separately?


recolor(Blockly.Blocks['variables_get'], myhue);


function recolor(block, hue) { 

    var oldInit = block.init; 

    block.init = function() { 

      oldInit.call(this); 

      this.setColour(hue); 

    } 

  } 


Eva Nolan

unread,
Mar 26, 2018, 12:13:35 PM3/26/18
to Blockly


Rachel Fenichel

unread,
Mar 26, 2018, 6:31:47 PM3/26/18
to Blockly
What if you just stored the desired color as a mutation, and defined the blocks to correctly parse that mutation?  Then you can put it in the XML for the blocks in the toolbox.

On Monday, March 26, 2018 at 5:13:35 AM UTC-7, Eva Nolan wrote:


Eva Nolan

unread,
Mar 27, 2018, 6:43:08 PM3/27/18
to Blockly
That worked! Thanks for your help!
Reply all
Reply to author
Forward
0 new messages