how i can draw mesh topology in sprit kit using swift - graph

I am trying to draw mesh Topology (graph) in sprite kit using swift. I am new for sprite kit . Please give any suggestion or sample code.

So as I mentioned in the comments, I don't know how to make a perfect topography algorithm, however I did come up with something that can replicate you picture.
Basically you add a bunch of plots as SKNodes, then use the .position property to use as the start and end points for a line drawn with CGPath. From that path, you can create a SKShapeNode(path: CGPath).
I also added a custom button in here that uses delegation, but it is completely separate from the actual "guts" of the topography. It's just a button.
// Overly complex way of creating a custom button in SpriteKit:
protocol DrawLinesDelegate: class { func drawLines() }
// Clickable UI element that will draw our lines:
class DrawLinesButton: SKLabelNode {
weak var drawLinesDelegate: DrawLinesDelegate?
init(text: String, drawLinesDelegate: DrawLinesDelegate) {
super.init(fontNamed: "Chalkduster")
self.drawLinesDelegate = drawLinesDelegate
self.text = text
isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print(drawLinesDelegate)
drawLinesDelegate?.drawLines()
}
required init?(coder aDecoder: NSCoder) { fatalError("") }
override init() { super.init() }
};
class GameScene: SKScene, DrawLinesDelegate {
var plots = [SKShapeNode]()
// var lines = [SKShapeNode]() // This may be useful in a better algorithm.
var nodesDrawnFrom = [SKShapeNode]()
var drawLinesButton: DrawLinesButton?
func drawLine(from p1: CGPoint, to p2: CGPoint) {
let linePath = CGMutablePath()
linePath.move(to: p1)
linePath.addLine(to: p2)
let line = SKShapeNode(path: linePath)
line.strokeColor = .red
line.lineWidth = 5
// lines.append(line) // Again, may be useful in a better algo.
addChild(line)
}
func drawLines() {
// Remove all lines: // Again again, may be useful in a better algorithm.
/*
for line in lines {
line.removeFromParent()
lines = []
}
*/
// The plot that we will draw from:
var indexNode = SKShapeNode()
// Find indexNode then draw from it:
for plot in plots {
// Find a new node to draw from (the indexNode):
if nodesDrawnFrom.contains(plot) {
continue
} else {
indexNode = plot
}
// Draw lines to every other node (from the indexNode):
for plot in plots {
if plot === indexNode {
continue
} else {
drawLine(from: indexNode.position, to: plot.position)
nodesDrawnFrom.append(indexNode)
}
}
}
}
func addNode(at location: CGPoint) {
let plot = SKShapeNode(circleOfRadius: 50)
plot.name = String(describing: UUID().uuid)
plot.zPosition += 1
plot.position = location
plot.fillColor = .blue
plots.append(plot)
addChild(plot)
}
override func didMove(to view: SKView) {
drawLinesButton = DrawLinesButton(text: "Draw Lines", drawLinesDelegate: self)
drawLinesButton!.position.y = frame.minY + (drawLinesButton!.frame.size.height / 2)
addChild(drawLinesButton!)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = touches.first!.location(in: self)
addNode(at: location)
}
}
imperfect algo:
Here you can see that there are multiple lines being drawn from one to another when you had mid-points (that is something blocking a straight line):
You would need to add another entire section to the algorithm to check for this.
Another important thing to note is that SKShapeNode() is very unperformant, and it would be best to transform all of these to SpriteNodes, or to bit-blit the entire scene onto a static texture.
However, having them all as ShapeNodes give you the most flexibility, and is easiest to explain here.

Related

What CALayer draw(in ctx:) coordinates should be used to draw in the correct point with the correct scale?

I have a NSView which uses CALayers to created content and want to generate pdf output in vector format. Firstly is this possible, and secondly what is the logical coordinate system that Apple refers to in their documentation, and lastly how are you supposed to get CALayers default background and border properties to draw ? If I use these properties then they are not drawn into the context and if I draw the border then this duplicates the property settings. It's a bit confusing as to when you should or shouldn't use these properties, or whether one should use CALayers at all when creating vector output e.g. pdf document.
The view has the following layers:
The views layer which is larger than the drawing area
A drawing layer which contains an image and sublayers
Sublayers which have some vector drawing including text
The NSView class and CALayer subclasses are listed below.
The view layer and the drawing layer are drawn in the correct locations but all the drawing layer subviews are drawn in the wrong place and are the wrong size.
I assume this is because the drawing layer has a transform applied to is and the drawing below is not taking that into account. Is this the issue and how would I apply the layers transforms to the CGContext - if this is the solution - to get things in the right place?
class DrawingView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let context = NSGraphicsContext.current?.cgContext else {
return
}
// In theory should generate vector graphics
self.layer?.draw(in: context)
//Or
// Generates bitmap image
//self.layer?.render(in: context)
}
}
class DrawingLayer: CALayer {
override func draw(in ctx: CGContext) {
drawBorder(in: ctx)
if let subs = self.sublayers {
for sub in subs {
sub.draw(in: ctx)
}
}
}
func drawBorder(in ctx: CGContext){
let rect = self.frame
if let background = self.backgroundColor {
ctx.setFillColor(background)
ctx.fill(rect)
}
if let borders = self.borderColor {
ctx.setStrokeColor(borders)
ctx.setLineWidth(self.borderWidth)
ctx.stroke(rect)
}
}
}
Here is my solution - using different function to draw in PDF. I am still not sure I understand why the system doesn't behave consistently when drawing to pdf and to the screen.
EDIT: - Part of the problem it seems is that I was using the layers frame for calculating size/position in the draw() functions but when a layer gets transformed its frame changes size and shape to fit the original rotated rectangle !! So tip - save your original rectangle elsewhere and then your drawings won't suddenly go weird when you apply a rotation or use bounds!!
EDIT2: - So render(in:) works, "kind of" - some 3D transforms get ignored, such as CATransform3DMakeTranslation(). Rotation transform seems to work fortunately so just make sure you set the correct origin to avoid the need for the translate transform.
When drawing in the pdf the NSView's draw(dirtyRect:) function is called by the CALayers draw(in:) functions don't automatically get called so it seems you have to call them all the way up the hierarchy yourself, remembering to translate the coordinate system for each sublayer - if they are drawing in their local coordinate systems. And be aware that this does not guarantee vector output in the pdf - it seems some things like text gets rendered as bitmaps. If anyone has an explanation for how things behave and why please explain.
In any event I have just fallen back to using the CALayer.render(in:) function which causes the full layer hierarchy to be rendered - and still with some vector and some bitmapped elements !
class DrawingView: NSView {
var drawingLayer: DrawingLayer? {
return self.layer?.sublayers?[0] as? DrawingLayer
}
override func draw(_ dirtyRect: NSRect) {
//super.draw(dirtyRect)
guard let context = NSGraphicsContext.current?.cgContext else {
return
}
// In theory should generate vector graphics
self.drawingLayer?.drawPdf(in: context)
//Or
// Generates bitmap image
//self.layer?.render(in: context)
}
}
class DrawingLayer: CALayer {
var fillColor: CGColor?
var lineColor: CGColor?
var lineWidth: CGFloat = 0.5
var scale: CGFloat = 1.0
// No need for this since we don't need to call draw for each sublayer
// that gets done for us!
override func draw(in ctx: CGContext) {
print("drawing DrawingLayer \(name ?? "")")
// if let subs = self.sublayers {
// for sub in subs {
// sub.draw(in: ctx)
// }
// }
}
func drawPdf(in ctx: CGContext) {
ctx.saveGState()
ctx.translateBy(x: self.frame.minX, y: self.frame.minY)
if let subs = self.sublayers {
for sub in subs {
(sub as! NormalLayer).drawPdf(in: ctx)
}
}
ctx.restoreGState()
}
}
class NormalLayer: CALayer {
var fillColor: CGColor?
var lineColor: CGColor?
var lineWidth: CGFloat = 0.5
var scale: CGFloat = 1.0
override func draw(in ctx: CGContext) {
drawBorder(in: ctx)
}
func drawBorder(in ctx: CGContext){
let bds = self.bounds
let rect = CGRect(x: bds.minX*scale, y: bds.minY*scale, width: bds.width*scale, height: bds.height*scale)
print("drawing NormalLayer \(name ?? "") \(rect)")
if let background = self.fillColor {
ctx.setFillColor(background)
ctx.fill(rect)
}
if let borders = self.lineColor {
ctx.setStrokeColor(borders)
ctx.setLineWidth(self.lineWidth)
ctx.stroke(rect)
}
}
func drawPdf(in ctx: CGContext) {
ctx.saveGState()
ctx.translateBy(x: self.frame.minX, y: self.frame.minY)
drawBorder(in: ctx)
ctx.restoreGState()
}
}

HERE-SDK lite drag marker in MapView

I am just trying out the SDK Lite API and I am wondering how I can achieve to drag a MapMarker object from one place to another. I suggest, it works somehow with disabling the default onPan gesture, but actually the problem starts with picking an existing object.
Here is my code so far:
public void pickMarker(Point2D p) {
map.getGestures().disableDefaultAction(GestureType.PAN);
map.pickMapItems(p, 20f, pickMapItemsResult -> {
if (pickMapItemsResult != null) {
pickedMarker = pickMapItemsResult.getTopmostMarker();
} else {
map.getGestures().enableDefaultAction(GestureType.PAN);
}
});
}
public void dragMarker(Point2D p) {
if (pickedMarker != null) {
pickedMarker.setCoordinates(map.getCamera().viewToGeoCoordinates(p));
}
}
public boolean releaseMarker(Point2D p) {
map.getGestures().enableDefaultAction(GestureType.PAN);
if (pickedMarker != null) {
GeoCoordinates newCoordinates = map.getCamera().viewToGeoCoordinates(p);
pickedMarker.setCoordinates(newCoordinates);
pickedMarker = null;
return true;
}
return false;
}
while these functions are called on the three states of the onPanListener:
mapView.getGestures().setPanListener((gestureState, point2D, point2DUpdate, v) -> {
if (gestureState.equals(GestureState.BEGIN)) {
mapViewUIEngine.pickMarker(point2D);
}
if (gestureState.equals(GestureState.UPDATE)) {
mapViewUIEngine.dragMarker(point2DUpdate);
}
if (gestureState.equals(GestureState.END)) {
if (mapViewUIEngine.releaseMarker(point2DUpdate)) {
regionController.movePoint(0,
updateNewLocation(point2D, point2DUpdate);
}
}
});
From one of the developer in Github I now know, that the polygon is returned instead of the marker (which is lying on a polygon line, but how can I get the marker instead?
You can use map markers to precisely point to a location on the map.
The following method will add a custom map marker to the map:
MapImage mapImage = MapImageFactory.fromResource(context.getResources(), R.drawable.here_car);
MapMarker mapMarker = new MapMarker(geoCoordinates);
mapMarker.addImage(mapImage, new MapMarkerImageStyle());
mapView.getMapScene().addMapMarker(mapMarker);
For more details, please refer
https://developer.here.com/documentation/android-sdk/dev_guide/topics/map-items.html#add-map-markers

Haxe Map only retrieves `null`

I'm having trouble using Maps in Haxe. I'm trying to create a grid of Tile objects and add them to the Map using their index on the grid as a key. However, when I try to retrieve a Tile from the map using an index I always get a value of null.
Could someone explain why this is happening? I've never used a map before and I don't understand what the issue is. I'm currently using a multidimensional array to get the same functionality, but maps seem more convenient.
private function initTiles():Void
{
var tempTile:Tile;
tileMap = new Map();
for (i in 0...widthTiles)
{
for (j in 0...heightTiles)
{
tempTile = new Tile(i * 32, j * 32);
tileMap.set([i,j],tempTile);
}
}
}
The issue is that the you are not actually creating a multidimensional array, you are creating a single dimensional array where the key type is Array<Int>. If ever in doubt, you can use $type( tileMap ) to get the compiler to tell you what type it thinks you have.
In your case, you would get:
Map<Array<Int>,Tile>; // This is an ObjectMap, where the object is an Array
When what you really want is:
Map<Int, Map<Int,Tile>>; // This is an IntMap, each value holding another IntMap
The reason this is an issue can be seen with this line:
trace( [0,0] == [0,0] ); // False!
Basically, in Haxe equality of objects (including arrays) is based on if they are the exact same object, not if they hold the same values. In this case, you are comparing two different arrays. Even though they hold the same values, they are actually two different objects, and not equal. Therefore they don't make suitable keys for your map.
Here is a working sample for what you need to do:
class Test {
static function main() {
initTiles();
trace( tileMap[3][6] );
}
static var tileMap:Map<Int,Map<Int,Tile>>;
static function initTiles():Void {
var widthTiles = 10;
var heightTiles = 10;
tileMap = new Map();
for (i in 0...widthTiles) {
if ( tileMap[i]==null ) {
// Add the sub-map for this column
tileMap[i] = new Map();
}
for (j in 0...heightTiles) {
// Add the tile for this column & row
tileMap[i][j] = new Tile(i*32, j*32);
}
}
}
}
class Tile {
var x:Int;
var y:Int;
public function new(x:Int, y:Int) {
this.x = x;
this.y = y;
}
}
And to see it in action: http://try.haxe.org/#E14D5 (Open your browser console to see the trace).

SKNode scale from the touched point

I have added UIPinchGestureRecognizer to my scene.view to scale my content. I actually scale the parent node where all my visible contents reside. But I have problem though with scaling point. The thing is node scale from the lower-left corner. It's definitely not what I want. Do I have to write lots of code to be able to scale from the point where pinching occurs? Could you please give some hints as to what way to follow.
I have been working on the same problem and my solution is shown below. Not sure if it is the best way to do it, but so far it seems to work. I'm using this code to zoom in and out of an SKNode that has several SKSpriteNode children. The children all move and scale with the SKNode as desired. The anchor point for the scaling is the location of the pinch gesture. The parent SKScene and other SKNodes in the scene are not affected. All of the work takes place during recognizer.state == UIGestureRecognizerStateChanged.
// instance variables of MyScene.
SKNode *_mySkNode;
UIPinchGestureRecognizer *_pinchGestureRecognizer;
- (void)didMoveToView:(SKView *)view
{
_pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleZoomFrom:)];
[[self view] addGestureRecognizer:_pinchGestureRecognizer];
}
// Method that is called by my UIPinchGestureRecognizer.
- (void)handleZoomFrom:(UIPinchGestureRecognizer *)recognizer
{
CGPoint anchorPoint = [recognizer locationInView:recognizer.view];
anchorPoint = [self convertPointFromView:anchorPoint];
if (recognizer.state == UIGestureRecognizerStateBegan) {
// No code needed for zooming...
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint anchorPointInMySkNode = [_mySkNode convertPoint:anchorPoint fromNode:self];
[_mySkNode setScale:(_mySkNode.xScale * recognizer.scale)];
CGPoint mySkNodeAnchorPointInScene = [self convertPoint:anchorPointInMySkNode fromNode:_mySkNode];
CGPoint translationOfAnchorInScene = CGPointSubtract(anchorPoint, mySkNodeAnchorPointInScene);
_mySkNode.position = CGPointAdd(_mySkNode.position, translationOfAnchorInScene);
recognizer.scale = 1.0;
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
// No code needed here for zooming...
}
}
The following are helper functions that were used above. They are from the Ray Wenderlich book on Sprite Kit.
SKT_INLINE CGPoint CGPointAdd(CGPoint point1, CGPoint point2) {
return CGPointMake(point1.x + point2.x, point1.y + point2.y);
}
SKT_INLINE CGPoint CGPointSubtract(CGPoint point1, CGPoint point2) {
return CGPointMake(point1.x - point2.x, point1.y - point2.y);
}
SKT_INLINE GLKVector2 GLKVector2FromCGPoint(CGPoint point) {
return GLKVector2Make(point.x, point.y);
}
SKT_INLINE CGPoint CGPointFromGLKVector2(GLKVector2 vector) {
return CGPointMake(vector.x, vector.y);
}
SKT_INLINE CGPoint CGPointMultiplyScalar(CGPoint point, CGFloat value) {
return CGPointFromGLKVector2(GLKVector2MultiplyScalar(GLKVector2FromCGPoint(point), value));
}
I have translated ninefifteen's solution for Swift and Pinch Gestures. I spent a couple days trying to get this to work on my own. Thank goodness for ninefifteen's Obj-C post! Here is the Swift version that appears to be working for me.
func scaleExperiment(_ sender: UIPinchGestureRecognizer) {
var anchorPoint = sender.location(in: sender.view)
anchorPoint = self.convertPoint(fromView: anchorPoint)
let anchorPointInMySkNode = _mySkNode.convert(anchorPoint, from: self)
_mySkNode.setScale(_mySkNode.xScale * sender.scale)
let mySkNodeAnchorPointInScene = self.convert(anchorPointInMySkNode, from: _mySkNode)
let translationOfAnchorInScene = (x: anchorPoint.x - mySkNodeAnchorPointInScene.x, y: anchorPoint.y - mySkNodeAnchorPointInScene.y)
_mySkNode.position = CGPoint(x: _mySkNode.position.x + translationOfAnchorInScene.x, y: _mySkNode.position.y + translationOfAnchorInScene.y)
sender.scale = 1.0
}
Can't zoom I don't know why but the main problem is those SKT_INLINE. I've googled them and didn't found anything about 'em... The problem is when I copy/paste them in my project the compiler tells me I have to add an ";" right after them. I wonder if that's the reason that I can zoom.
In Swift 4, my SKScene adds the UIPinchGestureRecognizer to the view but passes handling of the pinch gesture off to one of its SKNode children that is created in the scene's init(), due to some reasons not relevant here. Anyhow, this is ninefifteen's answer from the perspective of what s/he calls _mySkNode. It also includes a little code to limit the zoom and does not use the convenience functions listed at the bottom of his post. The #objc part of the declaration allows the function to be used in #selector().
Here is what is in my SKScene:
override func didMove(to view: SKView) {
let pinchRecognizer: UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self.grid, action: #selector(self.grid.pinchZoomGrid))
self.view!.addGestureRecognizer(pinchRecognizer)
}
And this is the relevant section in my SKNode:
// Pinch Management
#objc func pinchZoomGrid(_ recognizer: UIPinchGestureRecognizer){
var anchorPoint: CGPoint = recognizer.location(in: recognizer.view)
anchorPoint = self.scene!.convertPoint(fromView: anchorPoint)
if recognizer.state == .began {
// No zoom code
} else if recognizer.state == .changed {
let anchorPointInGrid = self.convert(anchorPoint, from: self.scene!)
// Start section that limits the zoom
if recognizer.scale < 1.0 {
if self.xScale * recognizer.scale < 0.6 {
self.setScale(0.6)
} else {
self.setScale(self.xScale * recognizer.scale)
}
} else if recognizer.scale > 1.0 {
if self.xScale * recognizer.scale > 1.5 {
self.setScale(1.5)
} else {
self.setScale(self.xScale * recognizer.scale)
}
}
// End section that limits the zoom
let gridAnchorPointInScene = self.scene!.convert(anchorPointInGrid, from: self)
let translationOfAnchorPointInScene = CGPoint(x:anchorPoint.x - gridAnchorPointInScene.x,
y:anchorPoint.y - gridAnchorPointInScene.y)
self.position = CGPoint(x:self.position.x + translationOfAnchorPointInScene.x,
y:self.position.y + translationOfAnchorPointInScene.y)
recognizer.scale = 1.0
} else if recognizer.state == .ended {
// No zoom code
}
}

Flex 3 - Using custom tooltips

I'm using the tooltipManager as described HERE.
I want to display a custom tooltip for the buttons of a buttonbar.
The buttonbar's declaration is as follow:
<mx:ButtonBar id="topToolbar" height="30" dataProvider="{topToolbarProvider}"
iconField="icon" itemClick="topToolbarHandler(event)"
buttonStyleName="topButtonBarButtonStyle"
toolTipField="tooltip"/>
Until here, everything works fine. I do see the proper text displaying in a tooltip.
Then, I created a custom tooltip manager using the tutorial quoted previously:
public class TooltipsManager
{
private var _customToolTip:ToolTip;
public function TooltipsManager()
{
}
public function showToolTipRight(evt:MouseEvent, text:String):void
{
var pt:Point = new Point(evt.currentTarget.x, evt.currentTarget.y);
// Convert the targets 'local' coordinates to 'global' -- this fixes the
// tooltips positioning within containers.
pt = evt.currentTarget.parent.contentToGlobal(pt);
customToolTip = ToolTipManager.createToolTip(text, pt.x, pt.y, "errorTipRight") as ToolTip;
customToolTip.setStyle("borderColor", "0xababab");
// Move the tooltip to the right of the target
var xOffset:int = evt.currentTarget.width + 5;//(customToolTip.width - evt.currentTarget.width) / 2;
customToolTip.x += xOffset;
}
public function showToolTipAbove(evt:MouseEvent, text:String):void
{
var pt:Point = new Point(evt.currentTarget.x, evt.currentTarget.y);
// Convert the targets 'local' coordinates to 'global' -- this fixes the
// tooltips positioning within containers.
pt = evt.currentTarget.parent.contentToGlobal(pt);
customToolTip = ToolTipManager.createToolTip(text, pt.x, pt.y, "errorTipAbove") as ToolTip;
customToolTip.setStyle("borderColor", "#ababab");
// Move tooltip below target and add some padding
var yOffset:int = customToolTip.height + 5;
customToolTip.y -= yOffset;
}
public function showToolTipBelow(evt:MouseEvent, text:String):void
{
var pt:Point = new Point(evt.currentTarget.x, evt.currentTarget.y);
// Convert the targets 'local' coordinates to 'global' -- this fixes the
// tooltips positioning within containers.
pt = evt.currentTarget.parent.contentToGlobal(pt);
customToolTip = ToolTipManager.createToolTip(text, pt.x, pt.y, "errorTipBelow") as ToolTip;
customToolTip.setStyle("borderColor", "ababab");
// Move tooltip below the target
var yOffset:int = evt.currentTarget.height + 5;
customToolTip.y += yOffset;
}
// Remove the tooltip
public function killToolTip():void
{
ToolTipManager.destroyToolTip(customToolTip);
}
[Bindable]
public function get customTooltip():ToolTip { return _customToolTip; }
public function set customTooltip(t:ToolTip):void { _customToolTip = t; }
}
Now, this is where I start having issues...
I'm trying to get to use this custom tooltip, but I don't know how to get the buttonbar to take it into account.
I created a function to see when could I call the functions in my TooltipsManager:
public function showTopToolbarTooltip(e:ToolTipEvent):void{
trace('blabla');
}
But it would seem that it's never taken into account. I've put this function in different buttonbar's events: tooltipcreate, tooltipstart, tooltipend but nothing happens. Not a single trace...
Could anyone tell me where to call the function of my tooltipManager?
Thanks a lot for your help.
Kind regards,
BS_C3
Actually, skipped a part of the tutorial =_= Attaching the functions to the mouseover/out events... Sorry for that.

Resources