LESS CSS - returning a variable conditionally - css

I use LESS CSS, and already use the 'return' functionality.
.get-theme-color(#theme) {
#return: ~"#{mdc-theme-#{theme}}";
}
And use it to assign values this:
#var: .get-theme-color(primary)[];
However, I'm trying to return a value selectively using a mixin. I've tried various formats - this is what I'm currently trying to make work - with no success:
.get-property-box(#mask) when (length(#mask) >= 1) and (length(#mask) =< 4) {
#mask-len: length(#mask);
#val1: extract(#mask, 1);
#value: ~"#{val1}";
& when (#mask-len = 2) {
#val2: extract(#mask, 2);
#value: ~"#{val1} #{val2}";
}
& when (#mask-len = 3) {
#val2: extract(#mask, 2);
#val3: extract(#mask, 3);
#value: ~"#{val1} #{val2} #{val3}";
}
& when (#mask-len = 4) {
#val2: extract(#mask, 2);
#val3: extract(#mask, 3);
#val4: extract(#mask, 4);
#value: ~"#{val1} #{val2} #{val3} #{val4}";
}
#return: #value;
}
Here's an alternative version:
.get-property-box(#mask) when (length(#mask) >= 1) and (length(#mask) =< 4) {
#mask-len: length(#mask);
#val1: extract(#mask, 1);
#return: ~"#{val1}";
& when (#mask-len = 2) {
#val2: extract(#mask, 2);
#return: ~"#{val1} #{val2}";
}
& when (#mask-len = 3) {
#val2: extract(#mask, 2);
#val3: extract(#mask, 3);
#return: ~"#{val1} #{val2} #{val3}";
}
& when (#mask-len = 4) {
#val2: extract(#mask, 2);
#val3: extract(#mask, 3);
#val4: extract(#mask, 4);
#return: ~"#{val1} #{val2} #{val3} #{val4}";
}
}
And call it like this:
.mdc-shape(round) {
#mask: 4rem, 5rem;
border-radius: .get-property-box(#mask)[];
}
The output is:
border-radius: 4rem;
I know that the guards are working correctly (the relevant section is reached), but the return value isn't updated.
Anyone got advice as to how I can make this work?

Related

LESS mixin and get default value

I have this mixin:
// define variable for later use
.define(#prefix, #var) {
#defined: '#{prefix}_#{var}';
}
.for(#i, #n) {
.-each(#i)
}
.for(#n) when (isnumber(#n)) {
.for(1, #n)
}
.for(#i, #n) when not (#i = #n) {
.for((#i + (#n - #i) / abs(#n - #i)), #n);
}
// ............................................................
// .for-each
.for(#array) when (default()) {
.for-impl_(length(#array))
}
.for-impl_(#i) when (#i > 1) {
.for-impl_((#i - 1))
}
.for-impl_(#i) {
.-each(extract(#array, #i))
}
// getProperty mixin
.getProperty(#property, #var, #base: true) {
& when not (##var = false) and (#base = true) {
#{property}: ##var;
}
#custom: 'my_#{var}';
& when not (##custom = false) {
.for(##custom);
.-each(#name) {
.define(#name, #var);
& when not (##defined = false) {
.#{name} & {
#{property}: ##defined;
}
}
}
}
}
My variables are like:
#parallax-color:#ffffff;
#my_parallax-color: ~"page-1" ~"page-2";
#page-1_parallax-color:initial;
#page-2_parallax-color:initial;
I call it like:
.getProperty(color, parallax-color);
How to change my mixin when value = initial then generate default color:#ffffff; ?
How to not render when value = inherit for example?

Every other rectangle detects the collison in my Java FX game

Okay so I have this little mock up game of clash royale and when I click to spawn a character and move him across the bridge on pane, I have a enemy on the bridge and my character stops as he should.
BUT, if I click to spawn my second character BEFORE he gets to enemy then the FIRST character moves through enemy and goes to tower.
Now, what I want to do is detect for every character, and they should all act the same. Not too sure what is going on here but I am needing a little assistance as this is the first game I've really made before.
private void createCharacter(CHARACTER choosenCharacter) {
gamePane.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (elixir > 4) {
character = new ImageView(choosenCharacter.getUrl());
healthbar = new Rectangle(50.0, 10.0, Color.BLUE);
character.setLayoutX(event.getSceneX() - 40);
character.setLayoutY(event.getSceneY() - 110);
healthbar.setLayoutX(event.getSceneX() - 40);
healthbar.setLayoutY(event.getSceneY() - 110);
System.out.println(event.getSceneX() + "\n" + event.getSceneY());
elixir = elixir - 6;
if (detectCollision()){
gameTimer.stop();
}
createGameLoop(character, healthbar);
gamePane.getChildren().add(character);
gamePane.getChildren().add(healthbar);
}
}
});
}
private void moveCharacter(ImageView character, Rectangle health) {
if (character.getLayoutX() > 180 && character.getLayoutX() < 450) {
character.setLayoutX(character.getLayoutX() - 2);
}
if (health.getLayoutX() > 180 && health.getLayoutX() < 450) {
health.setLayoutX(health.getLayoutX() - 2);
}
if (character.getLayoutX() <= 200 && character.getLayoutY() > 2) {
character.setLayoutY(character.getLayoutY() - 2);
}
if (health.getLayoutX() <= 200 && health.getLayoutY() > 2) {
health.setLayoutY(health.getLayoutY() - 2);
}
if (character.getLayoutX() < 700 && character.getLayoutX() >= 450) {
character.setLayoutX(character.getLayoutX() + 2);
}
if (health.getLayoutX() < 700 && health.getLayoutX() >= 450) {
health.setLayoutX(health.getLayoutX() + 2);
}
if (character.getLayoutX() >= 700 && character.getLayoutY() > 2) {
character.setLayoutY(character.getLayoutY() - 2);
}
if (health.getLayoutX() >= 700 && health.getLayoutY() > 2) {
health.setLayoutY(health.getLayoutY() - 2);
}
}
private void createGameLoop(ImageView character, Rectangle health) {
gameTimer = new AnimationTimer() {
#Override
public void handle(long now) {
if (detectCollision()){
gameTimer.stop();
}
moveCharacter(character, health);
}
};
gameTimer.start();
}
As you can see it's not the cleanest code but I need some way to detect each instance of my game character and I'm just not able to think of how to solve this little problem of mine. Any suggestions on how to handle something like this would be greatfully appreciated. Thanks!

Pass mixin as mixin param in LESS

I have the the following mixin:
.image-ui-wave-pos (#num, #selected:0) when (#selected = 0) {
background-position: -(#image-ui-wave-width * #num) -28px;
}
I'd like to pass it to the following mixin as parameter:
.small-button (#pos-macro, #buttons, #i) when (#i > 0) {
#button: extract(#buttons, #i);
&.#{button} {
.pos-macro (#i);
}
.small-button(#pos-macro, #buttons, #i - 1);
}
as the parameter #pos-macro calling it like this:
.small-button (.image-ui-wave-pos, #wave-buttons);
But it doesn't compile. How to do so?

LESS - Create ID by looping through two variables

I am trying to create a loop that outputs all of the possible combinations between coordinates -2,-2 to 2,2. Is there any way to do this without creating multiple loops?
Desired Output
#p1x0,#p2x0,#p-1x0,#p-2x0,#p1x1,#p-1x-1,#p-1x1,#p1x-1,#p2x2,#p-2x-2,p2x-2,p-2x2,#p2x1,#p2x-1,#p1x2,#p1x-2,#p-2x1,#p-2x-1,#p-1x2,#p-1x-2,#p0x-1,#p0x-2,#p0x0,#p0x1,#p0x2{}
Current Attempt
#cube-side {
border:red;
}
.create-cubes(#n, #i: -2, #z: -2, #side-sum:#i + #z) when (#side-sum =< #n) {
& when (#i < #z) {
.create-cubes(#n, #i+1);
}
& when (#z < #i) {
.create-cubes(#n, #z+1);
}
#p#{i}x#{z}:extend(#cube-side) {}
}
.create-cubes(4);
Output
#cube-side,
#p-2x-2 {
border: red;
}
There's way to do this w/o any loops at all:
#cube-side {
border: red;
}
-2, -1, 0, 1, 2 {
#p&x&:extend(#cube-side) {}
}
Though for an arbitrary list of values just a nested loop is the simplest solution of course (see for example), e.g. (in "pure Less") something like:
.create-cubes(-2, 2);
.create-cubes(#min, #max) {
.i; .i(#i: #min) when (#i <= #max) {
.j; .i(#i + 1);
}
.j(#j: #min) when (#j <= #max) {
#p#{i}x#{j}:extend(#cube-side) {}
.j(#j + 1);
}
}

recursive method - not all code paths return a value!

it says not all code paths return a value
private string Fisrt(string nonTerminal)
{
for (int j = 0; j < 6; j++)
{
if (Tokens[j, 0] == nonTerminal)
{
if (char.IsLower((char)Tokens[j, 3][0]))
return (Tokens[j, 3]);
else
Fisrt(Tokens[j, 3]);
}
}
}
private string Fisrt(string nonTerminal)
{
for (int j = 0; j < 6; j++)
{
if (Tokens[j, 0] == nonTerminal)
{
if (char.IsLower((char)Tokens[j, 3][0]))
return (Tokens[j, 3]);
else
return Fisrt(Tokens[j, 3]);
/* ^ add a return here */
}
}
return SOMETHING;
/* ^ You also need to add some return value here */
}
You also need to decide what string value (or null) to return in the event your for loop exits normally.
For example, what if none of the Tokens[j, 0], with j values 0 to 5, is nonTerminal?
Or, if Tokens[j, 3][0] is never lowercase?
You should return the recursive step
`return First(Tokens[j, 3])`
and handle the cases outside the outer for and if.

Resources