-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java: working on measure function stuff
- Loading branch information
Showing
7 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
bindings/java/java/examples/java/com/dioxuslabs/taffy/Measure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.dioxuslabs.taffy; | ||
|
||
import com.dioxuslabs.taffy.common.Image.ImageContext; | ||
import com.dioxuslabs.taffy.common.Text; | ||
import com.dioxuslabs.taffy.common.Text.FontMetrics; | ||
import com.dioxuslabs.taffy.common.Text.TextContext; | ||
import com.dioxuslabs.taffy.enums.Display; | ||
import com.dioxuslabs.taffy.enums.FlexDirection; | ||
import com.dioxuslabs.taffy.geom.Size; | ||
import com.dioxuslabs.taffy.geom.measure.AvailableSpace; | ||
import com.dioxuslabs.taffy.geom.measure.Dimension; | ||
import com.dioxuslabs.taffy.style.Style; | ||
|
||
import java.util.List; | ||
|
||
import static com.dioxuslabs.taffy.common.Image.imageMeasureFunction; | ||
import static com.dioxuslabs.taffy.common.Text.LOREM_IPSUM; | ||
import static com.dioxuslabs.taffy.common.Text.textMeasureFunction; | ||
|
||
/** | ||
* Equivalent of taffy/examples/measure.rs | ||
*/ | ||
public class Measure { | ||
public static Size<Float> measureFunction( | ||
Size<Float> knownDimensions, | ||
Size<AvailableSpace> availableSpace, | ||
Object nodeContext, | ||
FontMetrics fontMetrics | ||
) { | ||
if (knownDimensions.bothAxisDefined()) { | ||
return new Size<>(knownDimensions.width(), knownDimensions.height()); | ||
} | ||
|
||
if (nodeContext instanceof TextContext nc) { | ||
return textMeasureFunction(knownDimensions, availableSpace, nc, fontMetrics); | ||
} else if (nodeContext instanceof ImageContext nc) { | ||
return imageMeasureFunction(knownDimensions, nc); | ||
} else { | ||
return Size.zero(Float.class); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
TaffyTree taffy = new TaffyTree(); | ||
|
||
FontMetrics fontMetrics = new FontMetrics(10f, 10f); | ||
|
||
long textNode = taffy.newLeafWithContext( | ||
Style.def(), | ||
new TextContext(LOREM_IPSUM, Text.WritingMode.HORIZONTAL) | ||
); | ||
|
||
long imageNode = taffy.newLeafWithContext( | ||
Style.def(), | ||
new ImageContext(400f, 300f) | ||
); | ||
|
||
long root = taffy.newWithChildren( | ||
Style.builder() | ||
.display(Display.FLEX) | ||
.flexDirection(FlexDirection.COLUMN) | ||
.size(new Size<>(Dimension.length(200), Dimension.auto())), | ||
List.of(textNode, imageNode) | ||
); | ||
|
||
// Compute layout and print result | ||
taffy.computeLayoutWithMeasure( | ||
root, | ||
Size.maxContentAvailableSize() | ||
// todo measure func | ||
); | ||
|
||
taffy.printTree(root); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
bindings/java/java/src/main/java/com/dioxuslabs/taffy/MeasureFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.dioxuslabs.taffy; | ||
|
||
import com.dioxuslabs.taffy.geom.Size; | ||
import com.dioxuslabs.taffy.geom.measure.AvailableSpace; | ||
import com.dioxuslabs.taffy.style.Style; | ||
|
||
public interface MeasureFunction { | ||
Size<Float> measure( | ||
Size<Float> knownDimensions, | ||
Size<AvailableSpace> availableSpace, | ||
long nodeId, | ||
Object nodeContext, | ||
Style style | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters