Scala: Equivalent of WPF Canvas and Polygon - swing

I'm developing a Hex based Game. Having wrestled with the limitations of the C# type system, on discovering Scala I knew I had to rewrite the application in Scala. I need a basic GUI to be able to develop the main functionality. I've been using a WPF Canvas with Polygon class. I also use Wpf Border class and Line class on the Canvas. I don't need most of the functionality of Wpf. I don't use Xaml. i just need to map the graphical objects to the desired coordinates, receive left and right mouse click events from them and put up tool tips and display context menus. I don't even need the Wpf context menu property as as I prefer to the context menus to be dynamic. I handle scrolling and zoom through my own code.
The best that I've found to make Polygons is the awt GeneralPath class. Although this is supposedly depreciated. I've started with Scala Swing, but the MainFrame class will not allow me to use the awt canvas class as content. Any help / recommendations appreciated

just override paint of some Component:
public class MyCanvasPanel extends JPanel{
...
#Override
public void paint(Graphics g){/* do your java2d stuff here*/}
}

Related

What is the equivalent of the Adobe Flash symbol library for Haxe?

I'm trying to cross over to HaxeFlixel after learning of Flash's demise, but I'm not entirely sure how to bridge my understanding between the two. I'm currently using Visual Studio Code on a Mac.
In Flash(now Animate CC), you have a library full of symbols:
You can right-click and edit those symbols, such as setting a base class:
I have all of the .as base classes in my project folder and each one controls the Symbol(s). When I'm in VSC, I can create .hx files and I have a folder dedicated to images with all my game bitmaps in it. From there, I do not know how to recreate my game. What is a library of symbols, that I can assign base classes to, when coding in Haxe?
You can use assets created in Animate with the Haxe library OpenFL. Here is an overview of how this can be done: http://blog.peteshand.net/load-swfswc-assets-into-haxe-flash-or-html5-targets/
Also you could read official guide: http://www.openfl.org/learn/tutorials/using-swf-assets/
There's no library as you know it from Flash in OpenFL. If you look for "native" OpenFL asset handling look at the openfl.Assets class. It provides functions to load and parse several asset file formats.
If you need a class for your HaxeFlixel project that allows you to extend the basic functionality of a sprite you can extend FlxSprite and load the graphic/sprite sheet inside the constructor:
class Block extends FlxSprite{
public function new(){
super();
loadGraphic("assets/images/block.png", false, 64, 64);
}
}
You don't want to extend but just load the bitmap/spritesheet into a FlxSprite? Not much of a difference:
var block = new FlxSprite();
block.loadGraphic("assets/images/block.png");
add(block);
See also:
http://haxecoder.com/post.php?id=52
http://haxeflixel.com/documentation/flxsprite/
http://haxeflixel.com/documentation/tutorial/ (it's excellent!)

Need some help getting started with OOD in ActionScript 3

So being new to AS3 but not development in general, during a recent project that I just started I hit an immediate snag where my normal method of OOD is causing errors in AS3.
I created a layer in Adobe Flash called code just to keep everything separate and in frame one under actions I used the following code to get started:
var GameContainerSize:int = 400;
class GameInfo {
var GameID:int;
var HomeTeam:String;
var VisitingTeam:String;
function GameInfo()
{
}
}
This simple code immediately causes an error though
Scene 1, Layer 'Code', Frame 1, Line 4 1131: Classes must not be nested.
And my best guess is this is because the timeline and all code on it exists within a class already. So what should I be doing if I want to develop this program using class objects, or is this even possible in flash?
You can't define classes on the timeline. Each timeline is exported as a class, var and function declarations become members of the class, and frame code is moved to its own frame functions which are called when the timeline reaches that frame. Thus the error is saying "you can't put a class in a class" because the timeline already is a class.
To define classes you must use external .as files with package blocks:
// GameInfo.as
package {
public class GameInfo {
public var GameID:int;
public var HomeTeam:String;
public var VisitingTeam:String;
public function GameInfo(){ }
}
}
In Flash Pro you can link a class to a timeline (document or symbols). Such a class must extend Sprite or MovieClip.
You can also refer to any of your classes from any frame script. For example, you could put on frame 1 of your main timeline:
var gameInfo:GameInfo = new GameInfo();
Typical OOP would involve using external .as class files for almost everything, with only minimal function calls on your timeline frames, like stop() at the end of a timeline or calling a custom function you've added to the class linked to the timeline.
I created a layer
That's not ideal. The problem is that it will not get you any closer to understanding the code, because it isn't code. It's a feature of the flash authoring environment. You might as well spend a day with the drawing tool drawing something.
to keep everything separate and in frame one under actions
Organisation is important, but layers aren't the way to go. As each class definition goes into its own external file, you have to create additional files anyway if you want to create more classes. And you want to create more classes because having only one is horrible object oriented design. Being consistent here is important. That's why people in the comments suggested to use a document class and have no code on any frames. All code is organised in classes. No exceptions1.
Having said all that, I highly advice against using Adobe Flash to learn As3. There's too much obfuscation going on behind the scenes. If you want to learn how to code As3, use a code editor. (or plain text editor + compiler if you prefer). Learning what settings dialog has to be adjusted in order to get what you want is not going to get you any closer to understanding OOD in As3.
I also see package, is this kind of like a namespace and does it need to be named, if not what is its purpose?
No, packages are packages and namespaces are namespaces. Apples and oranges.
Packages are used to organize classes just like a structure of
folders is used to organize the .as fiels of those classes. In fact,
the package is pretty much exactly that folder structure, for example:
flash.display is for all display related classes
flash.events is for events
A namespace allows you to control access to members of a class. Namespaces are very rarely used in As3. Here's an article from grant skinner about namespaces. Personally, I never needed to use namespaces. If you are jsut getting started, you can very well ignore them for now.
That sounds perfect! except I cannot get it to launch on my Win10 machine. I may just end up outsourcing this at this ratio
flash develop is the alternative editor. It's free, fast and lightweight.
my normal method of OOD
You want to extend your definition of normal with
always explicitly specify the access control modifiers
member names start with small letters
public class GameInfo {
private var gameID:int;
private var homeTeam:String;
private var visitingTeam:String;
function GameInfo()
{
}
}
1admittedly, there are top level functions that are pretty much free floating functions without a class. If you want to do oop, this is not what you want. It's for the occasional independent utility function. Stick to classes for now.

AS3 game element structure

I'm trying to figure out how best to setup a kind of universal game element class for my game. What I want to try and create is a structure similar too..
GameElementPositionMovement (root class)
->GameElementVisual (handles all the graphics)
->GameElementPersonality (handles game logic)
I then want to be able to set up different personalities (monster, hero, icon etc) just by creating an instance of GameElementPersonality, but in it's constructor also be able to setup the visual and positioning/movement aspects as well.
I mentioned this in another question, and the answer that came back was...
It seems that you need kind of 'data model' class to store logic and a
visual ('view') class. Visual class shouldn't inherit from data model,
it should use it. This is OOP related problem: IS vs HAS (inheritance
vs composition)
But I'm not sure if I understand that. The position/movement without any visual data, seems a good first root class, and then you add to that the visual aspects (GameElementVisual), and then finally you add in personality "traits" (GameElementPersonality) such as armour, damage, health etc
Therefore I'm keeping, the positioning/movement, visual and logic separate, and I presumed the heirachy that I've laid out would be the best way to do that, but is this not a good way to do this? should it be more flat? with the GameElementPositionMovement, creating both a visual and logic instance and storing that in itself?
You could create a structure similar to this:
(pseudocode)
ElementData
//it doesn't have to extend any particular class
//however it would be nice if it could dispatch events and register listeners
class ElementData implements IEventDispatcher
{
public function ElementData() //constructor
{
//do some stuff
}
public function setSomeProperty(value:int):void
{
//
}
public function doSomeCrazyStuff():void
{
//
}
}
ElementVisual
class ElementVisual extends MovieClip //or just Sprite or even DiplayObjectContainer
{
public function ElementVisual(elementData)
{
//constructor takes an instance of ElementData class
elementData.addEventListener(CHANGE, onDataChange)
elementData.doSomeCrazyStuff();
if (userCliked)
{
elementData.setSomeProperty(15);
}
//you can have here some interactions with user (keyboard, mouse)
//then it can communicate with elenemtData and 'listen' what it says.
}
function onDataChange
{
//react accordingly
}
}
some visual representation (you may need many of these)
class Monster extends ElementVisual
{
//do all the graphic, animations etc
}
Then you need a class to set up all the data, visuals etc… In simplest implementation it can be the 'document class'.
It's not a proper MVC model - it's a simple example to show the concept of decoupling logic from visualisation.
MVC is not the only solution, there are other so called 'design patterns' which may be useful...
Yeah the idea with MVC is to keep things decoupled, in your case you're ultimately smashing everything into one chain of inheritance where you'll end up with one type of object that inherits all the properties from another object that inherits all the properties from another object, so you'll have an instance of this thing that represents everything, which is why this isn't a great pattern to go with.
If instead you have a GameElementPositionMovement class (Your main class) create an instance of the GameElementVisual and in instance of GameElementPersonality (or multiple instances if need be). Then any time a change to a property is made in GameElementPersonality (or any in the collection if you choose to make a collection) could dispatch an event, the main class GameElementPositionMovement could listen for the dispatched event and when it gets it can set/pass the GameElementPersonality instance or array to the GameElementVisual. Then in the GameElementVisual you're just dealing with drawing based on the current model all the time, the model is separated from the view logic, you'd also probably want to handle control in a separate class or in GameElementPositionMovement. (control being the modification of the model, in this case it would probably also be where you register listeners for user events, keyboard, mouse whatever)
This way the model remains clean of control logic and view logic, it's a clear separation of what goes where and really only the view sort of depends on the model and the controller sort of depends on the view but if interfaces are established for what the model view controller each need to communicate with each other in this way you can swap out any of those parts with a new class that implements the interface (not likely an issue in a small game, but the design lends itself to this ability and therefor future scalability).

How to use robotlegs and signals without flex ie. pure as3

I'm try to put together a bare bones robotlegs-signals project but all the samples I've seen are flex ie
Index.mxml
<context:SignalCafeContext contextView="{this}"/>
SignalCafeContext.as
public class SignalCafeContext extends SignalContext
{
override public function startup():void
{
injector.mapSingleton.... etc etc
}
}
Is this possible to replace the mxml with another .as file - normally I would pass from the main class
context = new MyContext(this); // where this is DisplayObjectContainer
however super() takes no parameters in SignalContext so I might be missing something.
More Info:
libs:
as3-signals-v0.5.swc
robotlegs-framework-v1.03.swc
signals-extensions-SignalsCommandMap.swc
What you're trying would work in the current RobotLegs v.1 release (v.1.5.2). Context and its subclass SignalContext take optional params. The first param is your context view:
contextView:DisplayObjectContainer = null
Here's the SignalContext class extending Context.
Note, Context in Robotlegs 2 does not take parameters (source).
I imagine you need to start with a actionscript project instead of a flex project in FlashBuilder first.
Yes, your right, you just extend the Context class, as you can see in the basic HelloFlash robotlegs demo
mxml tags are just shorthand for actionscript classes. So I'd imagine you could start by taking a look at the auto-generated actionscript code. There is a flash builder compiler option that will let you see this. Using that as a template, you probably can't go too far wrong.

AS3 central event dispatcher shared between instances (not static)

I'm building a flash app (just AS3 with FlashDevelop) and I'm having some trouble keeping things loosely coupled around the event system. I've done a lot of reading about central event systems and static eventdispatchers but they don't quite fit the bill for me.
What I'm building is similar to a video player. I have a Player class which is the parent of all the other little parts of the app. The Player class extends Sprite and I've currently designed it so that you could instantiate multiple Player's and put them on the stage. I also have a Controller class which extends EventDispatcher and I dispatch all my events through this class. It's a central event class.
The problem is that I need to pass around a reference to this class so that all the other classes can dispatch and listen through it. Passing around the reference works but it's the exact opposite of loose coupling. I know I could make a static EventDispatcher that all the classes could see but then if I had two or three Player's on the stage they would all hear each others events.
How can I create a type of sandbox that will allow all the child classes of a Player instance to be aware of the central dispatcher without passing a reference or making it static?
I'd suggest to use my static dispatcher, this one has an ID mechanism that allows to tell which Player instance dispatches an event.
It turns out what I was really trying to understand was loose-coupling using something like dependency injection.
I never ended up doing this in any AS3 projects as I don't do much of it anyway. Having done more C# recently I more easily grasped this concept using libraries such as StructureMap and Ninject.
For AS3 you could use a framework like Robotlegs. This will probably change the way you code AS3 and may not be for all developers/situations.