Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
pixelrevision / PascalJSONCodingTests.swift
Last active May 17, 2022
Pascal to Camel json coding
View PascalJSONCodingTests.swift
import XCTest
struct SampleCodable: Codable {
let sampleVariable1: String
let sampleVariable2: String
let cAPPEDVariable3: String
let sample: String
}
class PascalJSONCoding: XCTestCase {
@pixelrevision
pixelrevision / PixelPerfectCam.cs
Last active Apr 20, 2021
Script for unity to create a pixel locked orthogonal camera
View PixelPerfectCam.cs
using UnityEngine;
/**
* A camera to help with Orthagonal mode when you need it to lock to pixels. Desiged to be used on android and retina devices.
*/
public class PixelPerfectCam : MonoBehaviour {
/**
* The target size of the view port.
*/
public Vector2 targetViewportSizeInPixels = new Vector2(480.0f, 320.0f);
/**
View DependencyContainer.swift
class DependencyContainer {
static let shared = DependencyContainer()
private var types: [String: Any] = [:]
func registerType<T: Any>(_ type: T.Type, named name: String? = nil, creator: @escaping (String) -> T) {
let key = name ?? String(describing: type)
types[key] = creator
}
View BitField.js
var BitField = function(){};
BitField.isSet = function(i, flag){
return (i & flag) === flag;
}
BitField.setFlag = function(i, flag){
return i | flag;
}
BitField.removeFlag = function(i, flag){
return i & ~flag;
}
@pixelrevision
pixelrevision / Observable.swift
Last active Dec 27, 2018
Swift observable protocol
View Observable.swift
import Foundation
public protocol Observable: class {
associatedtype ObservableTarget = AnyObject
func add(observer: ObservableTarget)
func remove(observer: ObservableTarget)
func dispatch(_ handler: (ObservableTarget) -> ())
@pixelrevision
pixelrevision / MiddlewareProcessor.swift
Created Dec 19, 2018
Protocol for common ReSwift middleware usecase
View MiddlewareProcessor.swift
import ReSwift
public protocol MiddlewareProcessor {
typealias GetStateFunction = () -> Any?
func preprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
func postprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
}
public extension MiddlewareProcessor {
@pixelrevision
pixelrevision / PixelLevelCollision.js
Last active Jan 23, 2018
PixelLevelCollision.js
View PixelLevelCollision.js
/*
Works with the alpha channel of an image to determine if there is collision with alpha
@parameter image - the image to check coordinate with.
@parameter sampling - (0.1-1.0) The percentage to downsample the image. Helps with performance and memory. Default 0.5
@parameter threshhold - (0.0-1.0) the threshhold to set the alpha for a hit. default 0.2
*/
var PixelLevelCollision = function(image, sampling, threshhold){
var self = this;
if(sampling === undefined){
View Sequence+Walk.swift
extension Sequence {
func walk(_ childSelector: (Element) -> [Element], handler: (Element) -> Void) {
for item in self {
handler(item)
let children = childSelector(item)
if children.count > 0 {
children.walk(childSelector, handler: handler)
}
}
View Sequence+RecursiveFlatten.swift
extension Sequence {
func recursiveFlatten(_ childSelector: (Element) -> [Element]) -> [Element] {
var output: [Element] = []
for item in self {
output.append(item)
let newItems = childSelector(item)
if newItems.count > 0 {
output += newItems.recursiveFlatten(childSelector)
}
View NavigationSample.swift
import UIKit
import PlaygroundSupport
class SampleController: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
title = "Sample View"
view = UIView()
view.backgroundColor = UIColor.green