Skip to content

Commit d88b8b3

Browse files
committed
First comparison operator for the code view
1 parent e6e0f82 commit d88b8b3

File tree

18 files changed

+398
-200
lines changed

18 files changed

+398
-200
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "theframework"
3-
version = "0.1.16"
3+
version = "0.1.17"
44
edition = "2021"
55
description = "A cross platform application UI framework."
66
license = "MIT"
@@ -16,7 +16,7 @@ maths-rs = { version = "0.2", features = ["short_types", "short_hand_constructor
1616
pixels = { version = "0.13.0", optional = true }
1717
winit = { version = "0.28.7", optional = true }
1818
winit_input_helper = { version = "0.14.1", optional = true }
19-
therenderer = { version = "0.1.1", optional = true }
19+
#therenderer = { version = "0.1.1", optional = true }
2020
# therenderer = { path = "../therenderer", optional = true }
2121
rust-embed = { version = "8", default-features = true, features = ["include-exclude"] }
2222
png = "0.17.8"
@@ -47,7 +47,7 @@ wasm-bindgen-futures = "0.4"
4747
web-sys = { version = "0.3", features = ["GpuTextureFormat"] }
4848

4949
[features]
50-
renderer = ["therenderer"]
50+
#renderer = ["therenderer"]
5151
ui = ["futures", "rfd", "flate2", "rayon"]
5252
code = []
5353
pixels_winit = ["pixels", "winit", "winit_input_helper"]

Readme.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ The **code** option adds a unique and powerful visual scripting system to TheFra
3131
* Easy-to-use via drag and drop.
3232
* Very fast as it compiles to native closures.
3333

34-
### Option: renderer
35-
36-
A fast state-based 2D renderer supporting varuous shapes and text. The development of the *renderer* option is currently on hold to focus on the *ui* first.
37-
3834
### Current Backends
3935

4036
* *Desktops* via [pixels](https://github.com/parasyte/pixels) and [winit](https://github.com/rust-windowing/winit). This is the default backend.

examples/circle/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
theframework = { path = "../..", features = ["renderer"] }
7+
theframework = { path = "../..", features = [] }
88
lazy_static = "1.4.0"
99

1010
[lib]

examples/circle/Readme.md

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1 @@
1-
# Circle
2-
3-
Draws and moves a circle based on user input.
4-
5-
## Running on the Desktop
6-
7-
```bash
8-
cargo run --release --package circle
9-
```
10-
11-
Will run the example on the Desktop utilizing pixels and winit.
12-
13-
## Running on the Web
14-
15-
Install the WASM32 target:
16-
17-
```bash
18-
rustup target add wasm32-unknown-unknown
19-
```
20-
21-
Build the project and start a local server to host it:
22-
23-
```bash
24-
cargo run-wasm --release --package circle
25-
```
26-
27-
Open http://localhost:8000/ in your browser to run the example.
28-
29-
To build the project without serving it:
30-
31-
```bash
32-
cargo run-wasm --release --build-only --package circle
33-
```
34-
35-
## Building for Xcode
36-
37-
To build for Xcode you need to uncomment the last three lines in the Cargo.toml file of the Circle example:
38-
39-
```toml
40-
[lib]
41-
name = "rustapi"
42-
crate-type = ["staticlib"]
43-
```
44-
45-
and than build to a static lib via
46-
47-
```bash
48-
cargo build --release --package circle
49-
```
50-
51-
Copy the resulting librust.a lib to the Xcode/TheFramework folder, open the project in Xcode and run or deploy it.
1+
Simple example of using TheFramework without any enabled features, i.e. just a window in which we draw a white circle.

examples/circle/src/circle.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,44 @@
11
use theframework::prelude::*;
22

3-
pub struct Circle {
4-
circle_id: u32,
5-
}
3+
pub struct Circle {}
64

75
impl TheTrait for Circle {
86
fn new() -> Self
97
where
108
Self: Sized,
119
{
12-
Self { circle_id: 0 }
13-
}
14-
15-
/// Init the scene by adding a shape to the world space
16-
fn init(&mut self, ctx: &mut TheContext) {
17-
// The world space always has the id of 0
18-
if let Some(world_space) = ctx.renderer.get_space_mut(0) {
19-
world_space.set_coord_system(Center);
20-
self.circle_id = world_space.add_shape(Disc);
21-
world_space.set_shape_property(self.circle_id, Normal, Color, vec![1.0, 1.0, 1.0, 1.0]);
22-
world_space.set_shape_property(self.circle_id, Normal, Radius, vec![100.0]);
23-
world_space.set_shape_property(
24-
self.circle_id,
25-
Selected,
26-
Color,
27-
vec![1.0, 0.0, 0.0, 1.0],
28-
);
29-
world_space.set_shape_property(self.circle_id, Selected, Radius, vec![120.0]);
30-
}
10+
Self {}
3111
}
3212

3313
/// Draw a circle in the middle of the window
3414
fn draw(&mut self, pixels: &mut [u8], ctx: &mut TheContext) {
35-
ctx.renderer.draw(pixels, ctx.width, ctx.height);
15+
ctx.draw.rect(
16+
pixels,
17+
&(0, 0, ctx.width, ctx.height),
18+
ctx.width,
19+
&[0, 0, 0, 255],
20+
);
21+
ctx.draw.circle(
22+
pixels,
23+
&(ctx.width / 2 - 100, ctx.height / 2 - 100, 200, 200),
24+
ctx.width,
25+
&[255, 255, 255, 255],
26+
100.0,
27+
)
3628
}
3729

38-
/// If the touch event is inside the circle, set the circle state to Selected
39-
fn touch_down(&mut self, x: f32, y: f32, ctx: &mut TheContext) -> bool {
40-
if let Some(world_space) = ctx.renderer.get_space_mut(0) {
41-
if let Some(shape_id) = world_space.get_shape_at(x, y) {
42-
world_space.set_shape_state(shape_id, Selected);
43-
} else {
44-
world_space.set_shape_state(self.circle_id, Normal);
45-
}
46-
}
47-
ctx.renderer.needs_update()
30+
/// Touch down event
31+
fn touch_down(&mut self, _x: f32, _y: f32, _ctx: &mut TheContext) -> bool {
32+
false
4833
}
4934

50-
/// Set the circle state to Selected.
51-
fn touch_up(&mut self, _x: f32, _y: f32, ctx: &mut TheContext) -> bool {
52-
if let Some(world_space) = ctx.renderer.get_space_mut(0) {
53-
world_space.set_shape_state(self.circle_id, Normal);
54-
}
55-
ctx.renderer.needs_update()
35+
/// Touch up event
36+
fn touch_up(&mut self, _x: f32, _y: f32, _ctx: &mut TheContext) -> bool {
37+
false
5638
}
5739

58-
/// Query if the renderer needs an update (tramsition animation ongoing etc.)
59-
fn update(&mut self, ctx: &mut TheContext) -> bool {
60-
ctx.renderer.needs_update()
40+
/// Query if the widget needs a redraw
41+
fn update(&mut self, _ctx: &mut TheContext) -> bool {
42+
false
6143
}
6244
}

examples/codeeditor/src/editor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ impl TheTrait for CodeEditor {
182182

183183
sandbox.insert_module(module.clone());
184184
module.execute(&mut sandbox);
185+
code_view.set_compiled(true);
185186
code_view.set_debug_module(
186187
sandbox.get_module_debug_module(module.id),
187188
);
188189
} else {
190+
code_view.set_compiled(false);
189191
code_view.set_debug_module(TheDebugModule::new());
190192
}
191193
}

src/thecode/thecodeatom.rs

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::thecodenode::TheCodeNodeData;
55
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
66
pub enum TheCodeAtom {
77
Assignment(String),
8+
Comparison(String),
89
Value(TheValue),
910
Add,
1011
Multiply,
@@ -20,20 +21,20 @@ pub enum TheCodeAtom {
2021
Return,
2122
EndOfExpression,
2223
EndOfCode,
23-
Switch,
24-
CaseCondition,
25-
CaseBody,
2624
}
2725

2826
impl TheCodeAtom {
2927
pub fn uneven_slot(&self) -> bool {
30-
matches!(self, TheCodeAtom::Assignment(_name))
28+
matches!(self, TheCodeAtom::Assignment(_))
29+
|| matches!(self, TheCodeAtom::Comparison(_))
3130
|| matches!(self, TheCodeAtom::Add)
3231
|| matches!(self, TheCodeAtom::Multiply)
3332
}
3433

3534
pub fn can_assign(&self) -> bool {
36-
matches!(self, TheCodeAtom::LocalSet(_name))
35+
matches!(self, TheCodeAtom::LocalSet(_))
36+
|| matches!(self, TheCodeAtom::ObjectSet(_, _))
37+
|| matches!(self, TheCodeAtom::Pulse)
3738
}
3839

3940
pub fn from_json(json: &str) -> Self {
@@ -46,6 +47,43 @@ impl TheCodeAtom {
4647

4748
pub fn to_node(&self, ctx: &mut TheCompilerContext) -> TheCodeNode {
4849
match self {
50+
TheCodeAtom::Comparison(_) => {
51+
let call: TheCodeNodeCall =
52+
|_stack: &mut Vec<TheValue>,
53+
data: &mut TheCodeNodeData,
54+
sandbox: &mut TheCodeSandbox| {
55+
if let Some(f) = data.sub_functions.first_mut() {
56+
if let Some(v) = f.execute(sandbox).pop() {
57+
if data.values[0] == v {
58+
println!("yes");
59+
if data.sub_functions.len() > 1 {
60+
_ = data.sub_functions[1].execute(sandbox).pop();
61+
}
62+
}
63+
64+
println!("Comparison: left {:?}, right: {:?}", data.values[0], v);
65+
}
66+
}
67+
68+
TheCodeNodeCallResult::Continue
69+
};
70+
71+
let mut node = TheCodeNode::new(
72+
call,
73+
TheCodeNodeData::location_values(ctx.node_location, vec![TheValue::Int(0)]),
74+
);
75+
76+
if let Some(function) = ctx.remove_function() {
77+
// let mut sandbox = TheCodeSandbox::new();
78+
// if let Some(v) = function.execute(&mut sandbox).pop() {
79+
// println!("{:?}", v);//
80+
// node.data.values[1] = v;
81+
// }
82+
node.data.sub_functions.push(function);
83+
}
84+
85+
node
86+
}
4987
// Generates a pulse gate.
5088
TheCodeAtom::Pulse => {
5189
let call: TheCodeNodeCall =
@@ -98,7 +136,7 @@ impl TheCodeAtom {
98136

99137
node
100138
}
101-
TheCodeAtom::Assignment(_op) => {
139+
TheCodeAtom::Assignment(_) => {
102140
let call: TheCodeNodeCall =
103141
|_stack: &mut Vec<TheValue>,
104142
_data: &mut TheCodeNodeData,
@@ -390,7 +428,6 @@ impl TheCodeAtom {
390428
_sandbox: &mut TheCodeSandbox| {
391429
if let Some(b) = stack.pop() {
392430
if let Some(a) = stack.pop() {
393-
println!("t {:?} {:?}", a, b);
394431
if let Some(result) = TheValue::add(&a, &b) {
395432
stack.push(result);
396433
} else {
@@ -438,33 +475,6 @@ impl TheCodeAtom {
438475
};
439476
TheCodeNode::new(call, TheCodeNodeData::location(ctx.current_location))
440477
}
441-
TheCodeAtom::Switch => {
442-
let call: TheCodeNodeCall =
443-
|_stack: &mut Vec<TheValue>,
444-
_data: &mut TheCodeNodeData,
445-
_sandbox: &mut TheCodeSandbox| {
446-
TheCodeNodeCallResult::Continue
447-
};
448-
TheCodeNode::new(call, TheCodeNodeData::location(ctx.current_location))
449-
}
450-
TheCodeAtom::CaseCondition => {
451-
let call: TheCodeNodeCall =
452-
|_stack: &mut Vec<TheValue>,
453-
_data: &mut TheCodeNodeData,
454-
_sandbox: &mut TheCodeSandbox| {
455-
TheCodeNodeCallResult::Continue
456-
};
457-
TheCodeNode::new(call, TheCodeNodeData::location(ctx.current_location))
458-
}
459-
TheCodeAtom::CaseBody => {
460-
let call: TheCodeNodeCall =
461-
|_stack: &mut Vec<TheValue>,
462-
_data: &mut TheCodeNodeData,
463-
_sandbox: &mut TheCodeSandbox| {
464-
TheCodeNodeCallResult::Continue
465-
};
466-
TheCodeNode::new(call, TheCodeNodeData::location(ctx.current_location))
467-
}
468478
}
469479
}
470480

@@ -485,9 +495,18 @@ impl TheCodeAtom {
485495
}
486496
}
487497

498+
pub fn to_color(&self) -> [u8; 4] {
499+
match self {
500+
Self::ObjectSet(_, _) => [36, 61, 92, 255],
501+
//[87, 112, 143, 255]
502+
_ => [174, 174, 174, 255],
503+
}
504+
}
505+
488506
pub fn to_kind(&self) -> TheCodeAtomKind {
489507
match self {
490508
TheCodeAtom::Assignment(_op) => TheCodeAtomKind::Equal,
509+
TheCodeAtom::Comparison(_op) => TheCodeAtomKind::Equal,
491510
TheCodeAtom::FuncDef(_name) => TheCodeAtomKind::Fn,
492511
TheCodeAtom::FuncArg(_name) => TheCodeAtomKind::Identifier,
493512
TheCodeAtom::FuncCall(_name) => TheCodeAtomKind::Identifier,
@@ -503,15 +522,13 @@ impl TheCodeAtom {
503522
TheCodeAtom::Multiply => TheCodeAtomKind::Star,
504523
TheCodeAtom::EndOfExpression => TheCodeAtomKind::Semicolon,
505524
TheCodeAtom::EndOfCode => TheCodeAtomKind::Eof,
506-
TheCodeAtom::Switch => TheCodeAtomKind::If,
507-
TheCodeAtom::CaseCondition => TheCodeAtomKind::If,
508-
TheCodeAtom::CaseBody => TheCodeAtomKind::If,
509525
}
510526
}
511527

512528
pub fn describe(&self) -> String {
513529
match self {
514530
TheCodeAtom::Assignment(op) => op.clone(),
531+
TheCodeAtom::Comparison(op) => op.clone(),
515532
TheCodeAtom::FuncDef(name) => name.clone(),
516533
TheCodeAtom::FuncArg(name) => name.clone(),
517534
TheCodeAtom::FuncCall(name) => name.clone(),
@@ -530,15 +547,13 @@ impl TheCodeAtom {
530547
TheCodeAtom::Multiply => "*".to_string(),
531548
TheCodeAtom::EndOfExpression => ";".to_string(),
532549
TheCodeAtom::EndOfCode => "Stop".to_string(),
533-
TheCodeAtom::Switch => "Switch".to_string(),
534-
TheCodeAtom::CaseCondition => "Case".to_string(),
535-
TheCodeAtom::CaseBody => ":".to_string(),
536550
}
537551
}
538552

539553
pub fn help(&self) -> String {
540554
match self {
541555
TheCodeAtom::Assignment(name) => format!("Assignment ({}).", name),
556+
TheCodeAtom::Comparison(name) => format!("Comparison ({}).", name),
542557
TheCodeAtom::FuncDef(name) => format!("Function definition ({}).", name),
543558
TheCodeAtom::FuncArg(name) => format!("Function argument ({}).", name),
544559
TheCodeAtom::FuncCall(name) => format!(
@@ -581,9 +596,6 @@ impl TheCodeAtom {
581596
TheCodeAtom::Multiply => "Operator ('*')".to_string(),
582597
TheCodeAtom::EndOfExpression => ";".to_string(),
583598
TheCodeAtom::EndOfCode => "Stop".to_string(),
584-
TheCodeAtom::Switch => "Switch statement.".to_string(),
585-
TheCodeAtom::CaseCondition => "Switch 'Case' statement.".to_string(),
586-
TheCodeAtom::CaseBody => "Switch 'Case' body.".to_string(),
587599
}
588600
}
589601

0 commit comments

Comments
 (0)