Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
intermediate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Oberaigner committed May 21, 2024
1 parent a2f1324 commit 481e69d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 44 deletions.
19 changes: 19 additions & 0 deletions godot/src/sprite_selector/auto_refresh_sprites.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends AutoRefresh

const Sprite: Resource = preload("res://src/sprite_selector/sprite.tscn")


func _refresh_contents():
var sprites = Api.sprite_get_all()
render_sprites(sprites)


func render_sprites(sprites):
var code_container := $VBoxIntems
for old in code_container.get_children():
code_container.remove_child(old)

for path in sprites:
var sprite = Sprite.instantiate()
sprite.setup(path)
code_container.add_child(sprite)
11 changes: 11 additions & 0 deletions godot/src/sprite_selector/button_add_sprite.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends Button


func _on_pressed():
$FileDialog.popup_centered()


func _on_file_dialog_file_selected(path):
print_debug("HFefe")
API.sprite_add(path)
print_debug("djuashduis")
7 changes: 7 additions & 0 deletions godot/src/sprite_selector/file_dialog.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends FileDialog


func _on_file_selected(path):
print("HELL")
API.sprite_add(path)
print("GOO")
15 changes: 4 additions & 11 deletions rust/compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ pub struct Commands {

#[derive(Clone)]
pub enum Statement {
Move(),
Message(Message),
Print(Expression),
}

#[allow(dead_code)]
#[derive(Clone)]
pub struct Message {
from: String,
to: String,
contents: Expression,
MoveRandomly(),
MoveRelative(i64, i64),
MoveTo(i64, i64),
Calc(Expression),
}

#[derive(Clone)]
Expand Down
20 changes: 5 additions & 15 deletions rust/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Ast {

impl Ast {
pub fn append(&mut self) {
self.statements.push(ast::Statement::Move());
self.statements.push(ast::Statement::MoveRandomly());
}

pub fn statements(&self) -> &[ast::Statement] {
Expand All @@ -31,24 +31,14 @@ impl Compilable for Ast {

for s in &self.statements {
match s {
ast::Statement::Move() => {
output.push_str("\tposition.x = 400\n\tposition.y=500\n");
ast::Statement::MoveRandomly() => {
output.push_str(" position.x = position.x + (randi() % 11) - 4\n");
output.push_str(" position.y = position.y + (randi() % 11) - 4\n");
}
_ => todo!("not implemented yet"),
}
}

//format!("{}", output.to_owned());

"extends Sprite2D
func _ready():
print(\"Reeady\")
func _process(_delta):
position.x = 200
position.y = 400
"
.to_owned()
output.to_string()
}
}
69 changes: 69 additions & 0 deletions rust/godot-plugin/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use godot::prelude::*;

use crate::GodotASTNode;

pub fn to_godot_ast(a: &compiler::ast::Statement) -> Gd<GodotASTNode> {
use compiler::ast::Statement;
let node = match a {
Statement::MoveRandomly() => GodotASTNode {
node_type: 1,
identifier: -1, // TODO
member_data: array![],
},
Statement::MoveRelative(x, y) => GodotASTNode {
node_type: 2,
identifier: -1, // TODO
member_data: varray![x, y],
},
Statement::MoveTo(x, y) => GodotASTNode {
node_type: 2,
identifier: -1, // TODO
member_data: varray![x, y],
},
Statement::Calc(x) => GodotASTNode {
node_type: 3,
identifier: -1,
member_data: varray![expression_to_godot_ast(x)],
},
};

Gd::from_object(node)
}

pub fn expression_to_godot_ast(expr: &compiler::ast::Expression) -> Gd<GodotASTNode> {
use compiler::ast::Expression;

let node = match expr {
Expression::IntLiteral(a) => GodotASTNode {
node_type: 1000,
identifier: -1,
member_data: varray![a], // Literal Int
},

Expression::Addition(a, b) => GodotASTNode {
node_type: 2000,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
},

Expression::Subtraction(a, b) => GodotASTNode {
node_type: 2001,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
},

Expression::Multiplication(a, b) => GodotASTNode {
node_type: 2002,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
},

Expression::Division(a, b) => GodotASTNode {
node_type: 2003,
identifier: -1,
member_data: varray![expression_to_godot_ast(a), expression_to_godot_ast(b)],
},
};

Gd::from_object(node)
}
36 changes: 18 additions & 18 deletions rust/godot-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Registers an Engine Singleton.
// Provides the Top-Level API to GDScript.

mod conversion;
mod signals;
mod state;

use godot::builtin::Array;
use godot::prelude::*;

Expand All @@ -13,9 +17,6 @@ use godot::engine::Engine;
use crate::signals::*;
use crate::state::*;

mod signals;
mod state;

// Register this plugin as a Godot Extension
struct MyExtension {}

Expand Down Expand Up @@ -58,7 +59,7 @@ impl Api {
}

#[func]
fn get_ast() -> Array<i64> {
fn get_ast() -> ASTArray {
let ast = ast().lock().unwrap();
ast.to_godot_ast()
}
Expand All @@ -81,27 +82,26 @@ impl Api {
}
}

type ASTArray = Array<Gd<GodotASTNode>>;

trait ToGodotAst {
fn to_godot_ast(&self) -> Array<i64>;
fn to_godot_ast(&self) -> ASTArray;
}

impl ToGodotAst for Ast {
fn to_godot_ast(&self) -> Array<i64> {
fn to_godot_ast(&self) -> ASTArray {
self.statements()
.iter()
.map(to_godot_ast)
.collect::<Array<i64>>()
.map(conversion::to_godot_ast)
.collect::<ASTArray>()
}
}

fn to_godot_ast(a: &compiler::ast::Statement) -> i64 {
use compiler::ast::Statement;
match a {
Statement::Move() => 1,
Statement::Print(_str) => 2,
Statement::Message(_str) => 3,
}
#[allow(dead_code)]
#[derive(GodotClass)]
#[class(init, base=Node)]
struct GodotASTNode {
node_type: i64,
identifier: i64,
member_data: VariantArray,
}

// -----------------------------------------------------------
//

0 comments on commit 481e69d

Please sign in to comment.