This commit is contained in:
Tomas Husak 2021-03-09 16:36:01 +01:00
Родитель c682ceb28e
Коммит 19832ca5a2
3 изменённых файлов: 94 добавлений и 112 удалений

Просмотреть файл

@ -60,12 +60,12 @@ class Background extends Entity
{ {
parent::__construct(["x" => 0, "y" => 0], $size); parent::__construct(["x" => 0, "y" => 0], $size);
$this["attributes"]["class"]->add("asteroids-background"); $this->attributes["class"]->add("asteroids-background");
$this["attributes"]["style"]["height"] = $size["height"] . "px"; $this->attributes["style"]["height"] = $size["height"] . "px";
$this["attributes"]["style"]["width"] = $size["width"] . "px"; $this->attributes["style"]["width"] = $size["width"] . "px";
$this["attributes"]["style"]["top"] = "0px"; $this->attributes["style"]["top"] = "0px";
$this["attributes"]["style"]["left"] = "0px"; $this->attributes["style"]["left"] = "0px";
} }
public static function createDefault() : Background public static function createDefault() : Background
@ -80,13 +80,13 @@ class Asteroid extends MovableEntity
{ {
parent::__construct($position, $direction, $size); parent::__construct($position, $direction, $size);
$this["attributes"]["class"]->add("asteroids-asteroid"); $this->attributes["class"]->add("asteroids-asteroid");
$this["attributes"]["style"]["height"] = $this->size["height"] . "px"; $this->attributes["style"]["height"] = $this->size["height"] . "px";
$this["attributes"]["style"]["z-index"] = 1; $this->attributes["style"]["z-index"] = 1;
$this["attributes"]["style"]["width"] = $this->size["width"] . "px"; $this->attributes["style"]["width"] = $this->size["width"] . "px";
$this["attributes"]["style"]["top"] = $this->position["y"] . "px"; $this->attributes["style"]["top"] = $this->position["y"] . "px";
$this["attributes"]["style"]["left"] = $this->position["x"] . "px"; $this->attributes["style"]["left"] = $this->position["x"] . "px";
} }
public static function createDefault(array $position) : Asteroid public static function createDefault(array $position) : Asteroid
@ -97,8 +97,8 @@ class Asteroid extends MovableEntity
public function move(float $time) : void public function move(float $time) : void
{ {
parent::move($time); parent::move($time);
$this["attributes"]["style"]["top"] = $this->position["y"] . "px"; $this->attributes["style"]["top"] = $this->position["y"] . "px";
$this["attributes"]["style"]["left"] = $this->position["x"] . "px"; $this->attributes["style"]["left"] = $this->position["x"] . "px";
} }
public function penetration(array $position, array $size) : bool public function penetration(array $position, array $size) : bool
@ -118,20 +118,20 @@ class Bullet extends MovableEntity
{ {
parent::__construct($position, $direction, $size); parent::__construct($position, $direction, $size);
$this["attributes"]["class"]->add("asteroids-bullet"); $this->attributes["class"]->add("asteroids-bullet");
$this["attributes"]["style"]["height"] = $this->size["height"] . "px"; $this->attributes["style"]["height"] = $this->size["height"] . "px";
$this["attributes"]["style"]["z-index"] = 1; $this->attributes["style"]["z-index"] = 1;
$this["attributes"]["style"]["width"] = $this->size["width"] . "px"; $this->attributes["style"]["width"] = $this->size["width"] . "px";
$this["attributes"]["style"]["top"] = $this->position["y"] . "px"; $this->attributes["style"]["top"] = $this->position["y"] . "px";
$this["attributes"]["style"]["left"] = $this->position["x"] . "px"; $this->attributes["style"]["left"] = $this->position["x"] . "px";
} }
public function move(float $time) : void public function move(float $time) : void
{ {
parent::move($time); parent::move($time);
$this["attributes"]["style"]["top"] = $this->position["y"] . "px"; $this->attributes["style"]["top"] = $this->position["y"] . "px";
$this["attributes"]["style"]["left"] = $this->position["x"] . "px"; $this->attributes["style"]["left"] = $this->position["x"] . "px";
} }
public static function createDefault(array $position) public static function createDefault(array $position)
@ -146,13 +146,13 @@ class Rocket extends MovableEntity
{ {
parent::__construct($position, $direction, $size); parent::__construct($position, $direction, $size);
$this["attributes"]["class"]->add("asteroids-rocket"); $this->attributes["class"]->add("asteroids-rocket");
$this["attributes"]["style"]["height"] = $this->size["height"] . "px"; $this->attributes["style"]["height"] = $this->size["height"] . "px";
$this["attributes"]["style"]["z-index"] = 1; $this->attributes["style"]["z-index"] = 1;
$this["attributes"]["style"]["width"] = $this->size["width"] . "px"; $this->attributes["style"]["width"] = $this->size["width"] . "px";
$this["attributes"]["style"]["top"] = $this->position["y"] . "px"; $this->attributes["style"]["top"] = $this->position["y"] . "px";
$this["attributes"]["style"]["left"] = $this->position["x"] . "px"; $this->attributes["style"]["left"] = $this->position["x"] . "px";
} }
public static function createDefault(array $position) public static function createDefault(array $position)
@ -166,8 +166,8 @@ class Rocket extends MovableEntity
public function move(float $time) : void public function move(float $time) : void
{ {
parent::move($time); parent::move($time);
$this["attributes"]["style"]["top"] = $this->position["y"] . "px"; $this->attributes["style"]["top"] = $this->position["y"] . "px";
$this["attributes"]["style"]["left"] = $this->position["x"] . "px"; $this->attributes["style"]["left"] = $this->position["x"] . "px";
} }
} }
@ -186,13 +186,13 @@ class Application extends \PhpBlazor\Tag
parent::__construct("div"); parent::__construct("div");
$this->gameSettings = $settings; $this->gameSettings = $settings;
$this["attributes"]["class"]->add("asteroids-app"); $this->attributes["class"]->add("asteroids-app");
$this["attributes"]["style"]["height"] = $this->gameSettings["height"] . "px"; $this->attributes["style"]["height"] = $this->gameSettings["height"] . "px";
$this["attributes"]["style"]["width"] = $this->gameSettings["width"] . "px"; $this->attributes["style"]["width"] = $this->gameSettings["width"] . "px";
$this["attributes"]["tabindex"] = 0; $this->attributes["tabindex"] = 0;
$this["attributes"]->addEvent("onkeydown", function($seq, $builder) {$builder->AddEventKeyboardCallback($seq, "onkeydown", function($e) {$this->HandleKeyDown($e);});}); $this->attributes->addEvent("onkeydown", function($seq, $builder) {$builder->AddEventKeyboardCallback($seq, "onkeydown", function($e) {$this->HandleKeyDown($e);});});
$this["attributes"]->addEvent("onkeyup", function($seq, $builder) {$builder->AddEventKeyboardCallback($seq, "onkeyup", function($e) {$this->HandleKeyUp();});}); $this->attributes->addEvent("onkeyup", function($seq, $builder) {$builder->AddEventKeyboardCallback($seq, "onkeyup", function($e) {$this->HandleKeyUp();});});
$this->addButtons(); $this->addButtons();
$this->initGame(); $this->initGame();
@ -201,41 +201,41 @@ class Application extends \PhpBlazor\Tag
public function addButtons() : void public function addButtons() : void
{ {
$button= new \PhpBlazor\Tag("button"); $button= new \PhpBlazor\Tag("button");
$button["attributes"]["style"]["position"] = "absolute"; $button->attributes["style"]["position"] = "absolute";
$button["attributes"]["style"]["top"] = "700px"; $button->attributes["style"]["top"] = "700px";
$button["attributes"]["style"]["left"] = "0px"; $button->attributes["style"]["left"] = "0px";
$button->attributes->addEvent("onmousedown", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmousedown", function($e) {$this->HandleMouseDownMoveRight();});});
$button->attributes->addEvent("onmouseup", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmouseup", function($e) {$this->HandleMouseUp();});});
$button->content[] = new \PhpBlazor\Text("Move Right");
$button["attributes"]->addEvent("onmousedown", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmousedown", function($e) {$this->HandleMouseDownMoveRight();});}); $this->content[] = $button;
$button["attributes"]->addEvent("onmouseup", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmouseup", function($e) {$this->HandleMouseUp();});});
$button["content"][] = new \PhpBlazor\Text("Move Right");
$this["content"][] = $button;
unset($button); unset($button);
$button= new \PhpBlazor\Tag("button"); $button= new \PhpBlazor\Tag("button");
$button["attributes"]["style"]["position"] = "absolute"; $button->attributes["style"]["position"] = "absolute";
$button["attributes"]["style"]["top"] = "700px"; $button->attributes["style"]["top"] = "700px";
$button["attributes"]["style"]["left"] = "100px"; $button->attributes["style"]["left"] = "100px";
$button["attributes"]->addEvent("onclick", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onclick", function($e) {$this->HandleFire();});}); $button->attributes->addEvent("onclick", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onclick", function($e) {$this->HandleFire();});});
$button["content"][] = new \PhpBlazor\Text("Fire"); $button->content[] = new \PhpBlazor\Text("Fire");
$this["content"][] = $button; $this->content[] = $button;
unset($button); unset($button);
$button= new \PhpBlazor\Tag("button"); $button= new \PhpBlazor\Tag("button");
$button["attributes"]["style"]["position"] = "absolute"; $button->attributes["style"]["position"] = "absolute";
$button["attributes"]["style"]["top"] = "700px"; $button->attributes["style"]["top"] = "700px";
$button["attributes"]["style"]["left"] = "200px"; $button->attributes["style"]["left"] = "200px";
$button["attributes"]->addEvent("onmousedown", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmousedown", function($e) {$this->HandleMouseDownMoveLeft();});}); $button->attributes->addEvent("onmousedown", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmousedown", function($e) {$this->HandleMouseDownMoveLeft();});});
$button["attributes"]->addEvent("onmouseup", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmouseup", function($e) {$this->HandleMouseUp();});}); $button->attributes->addEvent("onmouseup", function($seq, $builder) {$builder->AddEventMouseCallback($seq, "onmouseup", function($e) {$this->HandleMouseUp();});});
$button["content"][] = new \PhpBlazor\Text("Move Left"); $button->content[] = new \PhpBlazor\Text("Move Left");
$this["content"][] = $button; $this->content[] = $button;
unset($button); unset($button);
} }
@ -244,11 +244,11 @@ class Application extends \PhpBlazor\Tag
$this->bullets = array(); $this->bullets = array();
$this->asteroids = array(); $this->asteroids = array();
$this->background = Background::createDefault(); $this->background = Background::createDefault();
$this["content"][] = &$this->background; $this->content[] = &$this->background;
// 40 is a default height of rocket // 40 is a default height of rocket
$this->rocket = Rocket::createDefault(["x" => 0, "y" => $this->gameSettings["height"] - 140 ]); $this->rocket = Rocket::createDefault(["x" => 0, "y" => $this->gameSettings["height"] - 140 ]);
$this["content"][] = &$this->rocket; $this->content[] = &$this->rocket;
$this->time = 0; $this->time = 0;
} }
@ -362,7 +362,7 @@ class Application extends \PhpBlazor\Tag
{ {
$builder->OpenElement($startIndex++, $this->name); $builder->OpenElement($startIndex++, $this->name);
$startIndex = $this["attributes"]->writeWithTreeBuilder($builder, $startIndex); $startIndex = $this->attributes->writeWithTreeBuilder($builder, $startIndex);
$startIndex = $this->rocket->writeWithTreeBuilder($builder, $startIndex); $startIndex = $this->rocket->writeWithTreeBuilder($builder, $startIndex);
$startIndex = $this->background->writeWithTreeBuilder($builder, $startIndex); $startIndex = $this->background->writeWithTreeBuilder($builder, $startIndex);

Просмотреть файл

@ -45,9 +45,9 @@ namespace PhpBlazor
[PhpType] [PhpType]
public class Tag : iBlazorWritable public class Tag : iBlazorWritable
{ {
protected string name; public string name;
protected AttributeCollection attributes; public AttributeCollection attributes;
protected List<iBlazorWritable> content; public List<iBlazorWritable> content;
public Tag():this("div") public Tag():this("div")
{ } { }
@ -64,24 +64,6 @@ namespace PhpBlazor
this.name = name; this.name = name;
} }
public PhpAlias this[string index]
{
get
{
switch (index)
{
case "content":
return PhpValue.FromClass(content).AsPhpAlias();
case "attributes":
return PhpValue.FromClass(attributes).AsPhpAlias();
case "name":
return new PhpAlias(name);
default:
throw new ArgumentException();
}
}
}
#region iBlazorWritable #region iBlazorWritable
public int writeWithTreeBuilder(Context ctx, RenderTreeBuilder builder, int startIndex) public int writeWithTreeBuilder(Context ctx, RenderTreeBuilder builder, int startIndex)
{ {

Просмотреть файл

@ -10,88 +10,88 @@ namespace PhpBlazor
[PhpType] [PhpType]
public class RenderTreeBuilder public class RenderTreeBuilder
{ {
private Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder _builder; public Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder Builder;
PhpComponent _component; public PhpComponent Component;
public RenderTreeBuilder(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, PhpComponent component) public RenderTreeBuilder(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, PhpComponent component)
{ {
this._builder = builder; this.Builder = builder;
this._component = component; this.Component = component;
} }
public void OpenElement(int sequence, string elementName) => _builder.OpenElement(sequence, elementName); public void OpenElement(int sequence, string elementName) => Builder.OpenElement(sequence, elementName);
public void CloseElement() => _builder.CloseElement(); public void CloseElement() => Builder.CloseElement();
public void AddAttribute(int sequence, string name, string? value) => _builder.AddAttribute(sequence, name, value); public void AddAttribute(int sequence, string name, string? value) => Builder.AddAttribute(sequence, name, value);
public void AddContent(int sequence, string textContent) => Builder.AddContent(sequence, textContent);
public void AddMarkupContent(int sequence, string textContent) => Builder.AddMarkupContent(sequence, textContent);
public void AddContent(int sequence, string textContent) => _builder.AddContent(sequence, textContent);
//TODO: Implement reminding function from RenderTreeBuilder...
#region events #region events
public void AddEventCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create(_component, new Action(() => value.Invoke(ctx)))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create(Component, new Action(() => value.Invoke(ctx))));
} }
public void AddEventClipboardCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventClipboardCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ClipboardEventArgs>(_component, new Action<ClipboardEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ClipboardEventArgs>(Component, new Action<ClipboardEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventDragCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventDragCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<DragEventArgs>(_component, new Action<DragEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<DragEventArgs>(Component, new Action<DragEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventErrorCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventErrorCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ErrorEventArgs>(_component, new Action<ErrorEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ErrorEventArgs>(Component, new Action<ErrorEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventEventCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventEventCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<EventArgs>(_component, new Action<EventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<EventArgs>(Component, new Action<EventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventFocusCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventFocusCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<FocusEventArgs>(_component, new Action<FocusEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<FocusEventArgs>(Component, new Action<FocusEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventChangeCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventChangeCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ChangeEventArgs>(_component, new Action<ChangeEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ChangeEventArgs>(Component, new Action<ChangeEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventKeyboardCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventKeyboardCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<KeyboardEventArgs>(_component, new Action<KeyboardEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<KeyboardEventArgs>(Component, new Action<KeyboardEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventMouseCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventMouseCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<MouseEventArgs>(_component, new Action<MouseEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<MouseEventArgs>(Component, new Action<MouseEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventPointerCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventPointerCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<PointerEventArgs>(_component, new Action<PointerEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<PointerEventArgs>(Component, new Action<PointerEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventWheelCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventWheelCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<WheelEventArgs>(_component, new Action<WheelEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<WheelEventArgs>(Component, new Action<WheelEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventProgressCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventProgressCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ProgressEventArgs>(_component, new Action<ProgressEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<ProgressEventArgs>(Component, new Action<ProgressEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
public void AddEventTouchCallback(Context ctx, int sequence, string name, IPhpCallable value) public void AddEventTouchCallback(Context ctx, int sequence, string name, IPhpCallable value)
{ {
_builder.AddAttribute(sequence, name, EventCallback.Factory.Create<TouchEventArgs>(_component, new Action<TouchEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e))))); Builder.AddAttribute(sequence, name, EventCallback.Factory.Create<TouchEventArgs>(Component, new Action<TouchEventArgs>((e) => value.Invoke(ctx, PhpValue.FromClass(e)))));
} }
#endregion #endregion
} }