Skip to content

Commit 3ee1207

Browse files
committed
adding iMobileViewer code
1 parent 9224d31 commit 3ee1207

28 files changed

+3964
-15
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "jni/libembroidery"]
2+
path = jni/libembroidery
3+
url = https://github.com/Embroidermodder/libembroidery

EmbroideryProto.xcodeproj/project.pbxproj

+387
Large diffs are not rendered by default.

EmbroideryProto.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

EmbroideryProto/AppDelegate.swift

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import UIKit
2+
3+
@UIApplicationMain
4+
class AppDelegate: UIResponder, UIApplicationDelegate {
5+
var window: UIWindow?
6+
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
7+
//url contains a URL to the file your app shall open
8+
9+
//In my EXAMPLE I would want to read the file as a dictionary
10+
//let dictionary = NSDictionary(contentsOfURL: url)
11+
let controller = self.window?.rootViewController as! EmbroideryViewController
12+
13+
controller.loadFile(url)
14+
return true
15+
}
16+
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
18+
// Override point for customization after application launch.
19+
return true
20+
}
21+
22+
func applicationWillResignActive(_ application: UIApplication) {
23+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24+
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25+
}
26+
27+
func applicationDidEnterBackground(_ application: UIApplication) {
28+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30+
}
31+
32+
func applicationWillEnterForeground(_ application: UIApplication) {
33+
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34+
}
35+
36+
func applicationDidBecomeActive(_ application: UIApplication) {
37+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38+
}
39+
40+
func applicationWillTerminate(_ application: UIApplication) {
41+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42+
}
43+
44+
45+
}
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "29x29",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "29x29",
11+
"scale" : "3x"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"size" : "40x40",
16+
"scale" : "2x"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"size" : "40x40",
21+
"scale" : "3x"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"size" : "60x60",
26+
"scale" : "2x"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"size" : "60x60",
31+
"scale" : "3x"
32+
},
33+
{
34+
"idiom" : "ipad",
35+
"size" : "29x29",
36+
"scale" : "1x"
37+
},
38+
{
39+
"idiom" : "ipad",
40+
"size" : "29x29",
41+
"scale" : "2x"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "40x40",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "40x40",
51+
"scale" : "2x"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"size" : "76x76",
56+
"scale" : "1x"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"size" : "76x76",
61+
"scale" : "2x"
62+
}
63+
],
64+
"info" : {
65+
"version" : 1,
66+
"author" : "xcode"
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
6+
</dependencies>
7+
<scenes>
8+
<!--View Controller-->
9+
<scene sceneID="EHf-IW-A2E">
10+
<objects>
11+
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
12+
<layoutGuides>
13+
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
14+
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
15+
</layoutGuides>
16+
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
17+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
20+
</view>
21+
</viewController>
22+
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
23+
</objects>
24+
<point key="canvasLocation" x="53" y="375"/>
25+
</scene>
26+
</scenes>
27+
</document>
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="15G1212" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
6+
<dependencies>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
9+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
10+
</dependencies>
11+
<scenes>
12+
<!--Embroidery View Controller-->
13+
<scene sceneID="tne-QT-ifu">
14+
<objects>
15+
<viewController id="BYZ-38-t0r" customClass="EmbroideryViewController" customModule="EmbroideryProto" customModuleProvider="target" sceneMemberID="viewController">
16+
<layoutGuides>
17+
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
18+
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
19+
</layoutGuides>
20+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
21+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
22+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
23+
<subviews>
24+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="BBs-7z-TuY" customClass="StitchView" customModule="EmbroideryProto" customModuleProvider="target">
25+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
26+
<subviews>
27+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="lnY-H6-7ch">
28+
<rect key="frame" x="139" y="80" width="97" height="45"/>
29+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
30+
<state key="normal" title="Draw It"/>
31+
<connections>
32+
<action selector="btnDraw:" destination="BYZ-38-t0r" eventType="touchUpInside" id="mbX-lH-S9a"/>
33+
</connections>
34+
</button>
35+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="https://github.com/Embroidermodder/Embroidermodder/blob/master/test-designs/Star.PCS?raw=true" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="ore-6l-4nn">
36+
<rect key="frame" x="25" y="8" width="312" height="30"/>
37+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
38+
<nil key="textColor"/>
39+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
40+
<textInputTraits key="textInputTraits"/>
41+
</textField>
42+
</subviews>
43+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
44+
</view>
45+
</subviews>
46+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
47+
<constraints>
48+
<constraint firstItem="BBs-7z-TuY" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="3lq-hH-Y0P"/>
49+
<constraint firstItem="BBs-7z-TuY" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="Bzw-dV-vIH"/>
50+
<constraint firstAttribute="trailing" secondItem="BBs-7z-TuY" secondAttribute="trailing" id="STu-h5-RcT"/>
51+
<constraint firstItem="BBs-7z-TuY" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="gTg-Ue-Zeu"/>
52+
</constraints>
53+
</view>
54+
<connections>
55+
<outlet property="stitchView" destination="BBs-7z-TuY" id="7u4-tQ-2xS"/>
56+
<outlet property="txtFile" destination="ore-6l-4nn" id="g8l-dY-ExT"/>
57+
</connections>
58+
</viewController>
59+
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
60+
</objects>
61+
<point key="canvasLocation" x="24.800000000000001" y="34.632683658170919"/>
62+
</scene>
63+
</scenes>
64+
</document>

EmbroideryProto/BinaryHelper.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import UIKit
2+
3+
class BinaryHelper {
4+
class func load(url: URL, completion: @escaping (_ filename: InMemoryFile) -> ()) {
5+
let sessionConfig = URLSessionConfiguration.default
6+
let session = URLSession(configuration: sessionConfig)
7+
let request = URLRequest(url: url)
8+
9+
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
10+
if let tempLocalUrl = tempLocalUrl, error == nil {
11+
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
12+
print("Success: \(statusCode)")
13+
}
14+
var data : Data
15+
do {
16+
try data = FileHandle(forReadingFrom: tempLocalUrl).readDataToEndOfFile()
17+
let file = InMemoryFile(contents: data)
18+
file.filename = (response?.suggestedFilename)!
19+
completion(file)
20+
} catch (let writeError) {
21+
print("error \(writeError)")
22+
}
23+
24+
} else {
25+
print("Failure: %@", error?.localizedDescription ?? "");
26+
}
27+
}
28+
task.resume()
29+
}
30+
}

EmbroideryProto/EmbPattern.swift

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import UIKit
2+
3+
public class EmbPattern {
4+
public var blocks = [StitchBlock]()
5+
private var currentPoint = CGPoint()
6+
private var currentBlock = StitchBlock()
7+
private var threadList = [UIColor]()
8+
9+
func addThread(_ thread : UIColor) {
10+
threadList.append(thread)
11+
}
12+
13+
func addStitchRel(point: CGPoint, stitchType: StitchType) {
14+
let tempPoint = CGPoint(x: point.x + currentPoint.x, y: point.y + currentPoint.y)
15+
self.addStitchAbs(point: tempPoint, stitchType: stitchType)
16+
}
17+
18+
func addStitchAbs(point: CGPoint, stitchType: StitchType) {
19+
if blocks.count == 0 {
20+
if(threadList.count == 0) {
21+
let newThread = UIColor(red: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), green: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), blue:CGFloat(Float(arc4random()) / Float(UINT32_MAX)), alpha: 1.0)
22+
self.threadList.append(newThread)
23+
currentBlock.Color = newThread
24+
} else{
25+
currentBlock.Color = self.threadList[0]
26+
}
27+
blocks.append(currentBlock)
28+
}
29+
if stitchType == StitchType.End {
30+
if self.currentBlock.Points.count == 0 {
31+
self.blocks.remove(at: self.blocks.count - 1)
32+
return
33+
}
34+
}
35+
if (stitchType.rawValue & StitchType.Stop.rawValue) > 0 {
36+
if self.currentBlock.Points.count == 0 {
37+
return
38+
}
39+
var threadIndex = 0
40+
let currIndex = self.threadList.firstIndex(of: self.currentBlock.Color)
41+
if currIndex == nil || (currIndex! + 1) >= self.threadList.count {
42+
let newThread = UIColor(red: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), green: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), blue:CGFloat(Float(arc4random()) / Float(UINT32_MAX)), alpha: 1.0)
43+
self.threadList.append(newThread)
44+
}
45+
if currIndex == nil {
46+
threadIndex = 0
47+
} else {
48+
threadIndex = currIndex! + 1
49+
}
50+
let sb = StitchBlock()
51+
self.currentBlock = sb
52+
sb.Color = self.threadList[threadIndex]
53+
self.blocks.append(sb)
54+
return
55+
}
56+
if (stitchType.rawValue & StitchType.Trim.rawValue) > 0 {
57+
currentPoint = point
58+
if self.currentBlock.Points.count == 0 {
59+
return
60+
}
61+
let currIndex = self.threadList.firstIndex(of: self.currentBlock.Color)
62+
let sb = StitchBlock()
63+
self.currentBlock = sb
64+
sb.Color = self.threadList[currIndex!]
65+
self.blocks.append(sb)
66+
return
67+
}
68+
currentPoint = point
69+
if (stitchType.rawValue & StitchType.Jump.rawValue) == 0 {
70+
self.currentBlock.Points.append(point)
71+
}
72+
}
73+
74+
func calculateBoundingBox() -> CGRect {
75+
var left = CGFloat.infinity
76+
var top = CGFloat.infinity
77+
var right = -CGFloat.infinity
78+
var bottom = -CGFloat.infinity
79+
for sb in self.blocks {
80+
let dim = sb.getBounds()
81+
left = min(left, dim.minX)
82+
top = min(top, dim.minY)
83+
right = max(right, dim.maxX)
84+
bottom = max(bottom, dim.maxY)
85+
}
86+
return CGRect(x: left, y: top, width: right - left, height: bottom - top)
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import UIKit
2+
3+
class EmbroideryViewController: UIViewController {
4+
5+
6+
@IBOutlet weak var txtFile: UITextField!
7+
@IBOutlet weak var stitchView: StitchView! {
8+
didSet {
9+
stitchView.addGestureRecognizer(UIPinchGestureRecognizer(
10+
target: stitchView,
11+
action: #selector(StitchView.changeScale(_:))
12+
))
13+
}
14+
}
15+
16+
17+
func loadFile(_ theurl : URL) {
18+
BinaryHelper.load(url: theurl,
19+
completion: {file in
20+
var pattern : EmbPattern
21+
let fileextension = String(file.filename.suffix(3)).lowercased()
22+
var reader : FormatProtocol? = nil
23+
if fileextension == "exp" {
24+
reader = FormatExp()
25+
} else if fileextension == "pcs" {
26+
reader = FormatPcs()
27+
} else if fileextension == "pec" {
28+
reader = FormatPec()
29+
} else if fileextension == "pes" {
30+
reader = FormatPes()
31+
}
32+
if reader != nil {
33+
pattern = (reader?.read(file: file))!
34+
} else {
35+
pattern = EmbPattern()
36+
}
37+
self.stitchView.setPattern(pattern)
38+
DispatchQueue.main.async {
39+
self.stitchView.setNeedsDisplay()
40+
}
41+
})
42+
}
43+
44+
@IBAction func btnDraw(_ sender: UIButton) {
45+
loadFile(URL(string: self.txtFile.text!)!)
46+
}
47+
}
48+

0 commit comments

Comments
 (0)